How to Restore Deleted Objects in S3 (Before You Panic)

TL;DR
- On a versioning-enabled bucket, a plain
aws s3 rmwrites a delete marker instead of destroying bytes. Delete that marker'sVersionIdand the object is live again in under a second. - If
get-bucket-versioningreturns{}or{"Status": "Suspended"}, there is no marker to remove and no prior version to copy. Recovery then depends entirely on replication, AWS Backup, or the original source files. - Almost every "we lost the whole prefix" incident I've worked traces back to a lifecycle rule expiring noncurrent versions after 1–7 days, not to a human typing
rm.
A DELETE in S3 is a write, not an unlink
On a filesystem, rm frees blocks. In S3 with versioning on, DELETE without a versionId inserts a new zero-byte object whose only property is "this key is deleted." The previous bytes are still there, billed, and readable if you know the version ID.
The confusion starts because every normal read path hides that. GET returns NoSuchKey, HEAD returns 404, and the console shows nothing under the prefix:
$ aws s3api head-object --bucket acme-assets-prod --key static/app.js --region us-east-1
An error occurred (404) when calling the HeadObject operation: Not Found
Add the version ID and the same call succeeds:
```bash
$ aws s3api head-object --bucket acme-assets-prod --key static/app.js
--version-id 3HL4kqtJlcpXroDTDmJ.rmSpXd3dIbrHY --region us-east-1
{
"AcceptRanges": "bytes",
"ContentLength": 184320,
"LastModified": "2026-02-03T09:12:44+00:00",
"ETag": ""9b2cf535f27731c974343645a3985328"",
"VersionId": "3HL4kqtJlcpXroDTDmJ.rmSpXd3dIbrHY"
}
Two things to internalize before touching anything: the 404 is a lie, and the object you want may be *behind* the delete marker rather than *at* the latest version. Those are different repair paths.
## Five configuration mistakes that turn a delete into data loss
| Mistake | Symptom | Cause | Prevention |
|---|---|---|---|
| Versioning never enabled | `get-bucket-versioning` → `{}` | Bucket created by hand or by an old template | Turn on versioning at creation in Terraform/CDK and assert it in CI |
| Versioning **Suspended** | `{"Status": "Suspended"}`; new PUTs all use `versionId: null` | Someone "cleaned up" to stop paying for versions | Never suspend — use lifecycle expiration instead |
| `NoncurrentVersionExpiration: NoncurrentDays: 1` | Old versions vanish 24h after being replaced | Retention shorter than your recovery SLA | 30–90 days minimum; treat the JSON as production code |
| IAM grants `s3:DeleteObject` but not `s3:DeleteObjectVersion` | Restore fails with `AccessDenied` | Policy written for the common case only | Grant both, scoped to the specific prefix |
| MFA Delete or Object Lock retention | 403 even with full admin | Compliance control, working as designed | Document who holds the MFA device and the retention window |
The Suspended case is the nastiest because it looks safe. A bucket in `Suspended` state keeps old versions, but every new `PUT` writes to the literal version ID `null` and overwrites the previous null version in place. You end up with a bucket that "has versioning" and still silently loses the file you just edited.
## Restoring one object, in the right order
Work top-down and don't skip step 1. Guessing at the versioning state is how people delete the wrong thing.
```bash
# 1. Confirm versioning is Enabled (not Suspended)
aws s3api get-bucket-versioning --bucket acme-assets-prod --region us-east-1
# 2. Find the delete marker sitting on top of the key
aws s3api list-object-versions
--bucket acme-assets-prod
--prefix "static/app.js"
--query 'DeleteMarkers[?IsLatest==`true`].[VersionId,LastModified]'
--output text
--region us-east-1
# 3. Remove that marker — this is the actual restore
aws s3api delete-object
--bucket acme-assets-prod
--key "static/app.js"
--version-id "3HL4kqtJlcpXroDTDmJ.rmSpXd3dIbrHY"
--region us-east-1
# 4. Verify
aws s3api head-object --bucket acme-assets-prod --key "static/app.js" --region us-east-1
Step 3 looks like a destructive command and that's exactly what it is — you are deleting the tombstone, not the data. If the marker is not `IsLatest`, something has been written on top since the deletion and you need to think about which version should win.
Sometimes you don't want to un-delete; you want to roll back to a specific older version and leave history intact. Copy that version forward instead of deleting anything:
```bash
aws s3api copy-object
--bucket acme-assets-prod
--key "static/app.js"
--copy-source "acme-assets-prod/static/app.js?versionId=3HL4kqtJlcpXroDTDmJ%2BrmSpXd3dIbrHY"
--region us-east-1
Note the `%2B`. Version IDs are opaque strings containing `+`, `/` and `=`, and inside `--copy-source` they are parsed as a query string. An unencoded `+` becomes a space on the wire and the call dies with an `InvalidArgument`/`NoSuchVersion` error that looks nothing like an encoding problem. I burned twenty minutes on this the first time.
## Bulk restore when the prefix is gone
For a few hundred keys, a loop is fine. The CLI paginates `list-object-versions` automatically at 1,000 keys per page, so you don't need to handle `NextKeyMarker` yourself:
```bash
aws s3api list-object-versions
--bucket acme-assets-prod
--prefix "static/"
--query 'DeleteMarkers[?IsLatest==`true`].[Key,VersionId]'
--output text
--region us-east-1 |
while read -r key vid; do
aws s3api delete-object --bucket acme-assets-prod --key "$key" --version-id "$vid"
done
Past ~5,000 objects, stop looping and use `delete-objects`, which accepts up to 1,000 entries per call:
```bash
aws s3api delete-objects --bucket acme-assets-prod --region us-east-1
--delete file://markers.json
with `markers.json` shaped like `{"Objects": [{"Key": "static/app.js", "VersionId": "..."}]}`. Above 100k objects, generate a manifest from S3 Inventory and hand it to S3 Batch Operations — it retries, reports per-object status, and won't die halfway through when your laptop sleeps.
Throughput is not the bottleneck, by the way. S3 handles 3,500 DELETE/PUT requests per second per prefix. The bottleneck is your terminal.
## When NOT to use this
Delete-marker removal only helps when the versions still exist and you control the bucket. It is the wrong tool in three situations.
If credentials leaked, the attacker has the same permissions you do and will run `delete-objects` with explicit version IDs, or delete the bucket policy first. Object Lock in compliance mode and cross-account replication (destination owned by a different account) are the only controls that survive that.
If the deletion is a legal or GDPR erasure request, restoring is the violation, not the fix. Check for a legal hold before you touch anything.
And if `get-bucket-versioning` shows versioning was off for the object's whole life, stop scripting. Nothing exists to recover — go find the build artifact, the upstream system, or the last backup, and accept that hours of work may be gone.
## Troubleshooting: the delete marker I couldn't remove
2025-11-14, 02:41 UTC. Alerting fired because a static asset prefix started returning 404s on a production site. The bucket clearly had versioning on and the markers were clearly listed, but the removal command failed:
```bash
An error occurred (AccessDenied) when calling the DeleteObject operation: Access Denied
First instinct was a wrong profile. `aws sts get-caller-identity` returned the expected deploy role, so identity wasn't the issue. Then I dumped the policy attached to that role and read it slowly: it granted `s3:DeleteObject` on `arn:aws:s3:::acme-assets-prod/static/*`. That action covers ordinary deletes. Removing a delete marker is a *versioned* delete and needs `s3:DeleteObjectVersion`, which was missing entirely.
I confirmed the theory instead of guessing, using the policy simulator:
```bash
aws iam simulate-principal-policy
--policy-source-arn arn:aws:iam::123456789012:role/deploy-role
--action-names s3:DeleteObjectVersion
--resource-arns arn:aws:s3:::acme-assets-prod/static/app.js
It came back `implicitDeny`. Adding the action, re-running the delete, and re-running `head-object` returned HTTP 200 with the original `ContentLength` of 184320. Total outage: about 25 minutes.
The follow-up mattered more than the fix. While reading bucket config I found a lifecycle rule created three weeks earlier with `NoncurrentVersionExpiration: NoncurrentDays: 1`. Had the incident happened one day later, the version I restored would have been permanently gone. Reading lifecycle JSON is now the first thing I do in any S3 recovery ticket, before I look at IAM.
## Verification: close the ticket properly
A 200 from `head-object` is necessary but not sufficient. Confirm `list-object-versions` no longer shows an `IsLatest` delete marker for the key, and compare the ETag against your build manifest — a restore that brings back the wrong commit is still an incident.
Then check the CDN. CloudFront caches 404s under `ErrorCachingMinTTL`, and plenty of setups bump that to 300 seconds or more. The object is back in S3 while users still see the error page:
```bash
aws cloudfront create-invalidation
--distribution-id E1EXAMPLE123456
--paths "/static/app.js"
Last check: does the lifecycle rule that caused this still exist? If yes, you've scheduled the sequel.
We run this lifecycle-and-versioning audit for SMB buckets as part of awsgood.com managed services, and it's usually twenty minutes of reading JSON that prevents a 3am call.
## Sources
- [Deleting object versions from a versioning-enabled bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/DeleteMarker.html)
- [Retaining multiple versions of objects with S3 Versioning](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Versioning.html)
- [Expiring noncurrent object versions with lifecycle rules](https://docs.aws.amazon.com/AmazonS3/latest/userguide/lifecycle-configuration-examples.html)
- [delete-object — AWS CLI command reference](https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html)
FAQ
Can you restore a deleted S3 object if versioning was never enabled?
No. Without versioning, a DELETE unlinks the object permanently and S3 has no copy to hand back. Your only options are a replica bucket, an AWS Backup recovery point, or re-uploading from the source system. Enable versioning at bucket creation and assert it in your IaC tests so this never becomes a question.
Does removing a delete marker restore the object immediately?
Yes, effectively. Deleting the delete marker's VersionId makes the previous version current again and reads succeed within a second or two. There is no restore job and no retrieval charge, unlike Glacier restores. Just remember to invalidate any CDN cache that stored the 404.
How do I restore many deleted objects at once?
List delete markers with `aws s3api list-object-versions` filtered to `IsLatest`, then feed them to `aws s3api delete-objects` in batches of up to 1,000 per call. For more than 100k objects, build a manifest from S3 Inventory and run it through S3 Batch Operations so failures are reported per object.
Why does my object still return 404 after the delete marker is gone?
Almost always caching. CloudFront and other CDNs cache error responses according to `ErrorCachingMinTTL`, which is often configured well above the default. Confirm with `head-object` directly against the bucket, then create an invalidation for the affected paths.
Can someone delete my S3 versions and defeat versioning entirely?
Yes, if their credentials allow `s3:DeleteObjectVersion` or the bucket policy is deleted first. Versioning protects against accidents, not attackers. Add S3 Object Lock in compliance mode or replicate to a bucket in a separate account that the compromised principal cannot reach.
How long do I have before old S3 versions disappear?
It depends entirely on your lifecycle rules. If `NoncurrentVersionExpiration` is set to 7 days, the recoverable window is 7 days from the moment a version stops being current. If no rule exists, versions stay forever and you pay for every byte. Check with `get-bucket-lifecycle-configuration` before assuming you have time.
Leave a Reply