Recovering a Deleted Amazon S3 Object from a Versioned Bucket
Deleted an object from a versioned S3 bucket? Find the delete marker with list-object-versions, copy the last good version back with copy-object and check it.

Advertising disclosure: this article includes partner links. KBY may earn a commission if you sign up or buy through one, at no extra cost to you. How we make money.
Before you begin
- A non-production general purpose S3 bucket with Bucket Versioning enabled, at least 15 minutes after versioning was first turned on.
- The AWS CLI installed and configured for that account.
- s3:GetBucketVersioning, s3:ListBucketVersions, s3:GetObject, s3:GetObjectVersion, s3:PutObject and s3:DeleteObject on the lab bucket, plus s3:ListBucket.
Underlined words are defined in the KBY Lexicon. Tap or click one to see what it means.
If versioning is turned on for the bucket, a deleted Amazon S3 object is usually still there. A plain delete does not erase it. S3 adds a delete marker, which becomes the current version, so the object answers 404 Not Found and drops out of normal listings. To get it back, list its versions with aws s3api list-object-versions, find the last good version under the marker, and copy that version over the same key with aws s3api copy-object. Copying keeps every version and can be undone with another plain delete. The constraint to plan around: a version that was permanently deleted, by a delete that named its version ID or by a lifecycle rule, cannot be brought back from S3.
The idea underneath is that an S3 key in a versioned bucket is a stack of versions, not a single file. Reads go to whichever version is on top. Deleting, overwriting and restoring all add to the stack rather than taking from it, until someone removes a specific version on purpose. Once that picture is clear, the commands are short. The harder part is reading the version list correctly and knowing which kinds of delete cannot be undone.
#What you will learn from one deleted file
- Explain what a version ID, the current version and a delete marker are in an S3 bucket with versioning on.
- Tell the difference between a plain delete, which can be undone, and a delete that names a version ID, which cannot.
- Read the output of
list-object-versionsand pick the version to restore by its timestamp. - Restore an object by copying an earlier version over its key, and prove the result with
head-objectandget-object. - Name the permissions, lifecycle rules and size limits that decide whether a restore will work in production.
#What you’ll need in the AWS account
- A non-production general purpose S3 bucket you are allowed to write to, with Bucket Versioning set to Enable. Directory buckets do not support versioning, so they are not suitable.
- If versioning was turned on recently, wait 15 minutes before the first upload. AWS documents that the change can take that long to reach the whole of S3, and that reads in that window may return
404 NoSuchKey. - The AWS CLI installed and configured with credentials for that account. The examples use Unix-style quoting, so adjust the quotes in PowerShell or the Windows command prompt.
- An identity with
s3:GetBucketVersioning,s3:ListBucketVersions,s3:GetObject,s3:GetObjectVersion,s3:PutObjectands3:DeleteObjecton that bucket, pluss3:ListBucketso that a missing object shows as 404 rather than 403. Nothing in the lab needss3:DeleteObjectVersion. - A key prefix of your own for the lab, such as
kby-lab/, so you never touch anyone else’s objects.
#Why a deleted S3 object is usually still there
#Versions, the current version and the delete marker
By default an S3 bucket is unversioned. Once versioning is enabled, the bucket can never go back to being unversioned; it can only be suspended. From then on, every write to a key gets its own version ID, and the newest one is the current version. Objects that were already in the bucket keep a version ID of null. Each version is a whole copy of the object, not a difference from the one before, and each is charged as a full object.
A delete marker is a placeholder that S3 creates for a key when it receives a delete request without a version ID. It has a key and a version ID like any object, but it holds no data. When a delete marker is the current version, a GET without a version ID returns 404 Not Found with the header x-amz-delete-marker: true, and ListObjectsV2 leaves the key out. That is why the object looks gone in the console and in scripts that list the bucket. The data versions underneath are untouched.

#Two kinds of delete, with very different results
A simple delete is one that does not name a version ID. In a versioned bucket it never removes data; it only adds a delete marker on top of the stack. Sending another simple delete while the marker is current does not remove the marker either. S3 adds a second marker.
A delete that names a version ID is different. It permanently removes that one version, whether it is a data version or a delete marker, and AWS states that this cannot be undone. It needs the separate s3:DeleteObjectVersion permission. Lifecycle rules can do the same thing on a schedule: the NoncurrentVersionExpiration action permanently removes noncurrent versions, and those cannot be recovered. So the honest answer to “can I get it back?” depends on whether a data version is still listed, not on how long ago the delete happened.
#Two documented ways back
AWS documents two ways to make an earlier version current again. The first is to copy the earlier version into the same bucket under the same key. The copy becomes a new current version with a new version ID, and every existing version, including the delete marker, is kept. The second is to remove the delete marker by deleting it by its version ID, which leaves the most recent data version on top. The S3 console’s undelete works this second way: you turn on the Show versions switch (some AWS pages call it List versions), select the delete marker, choose Delete and type permanently delete to confirm.
This lab uses the copy. It needs no delete permission, it keeps the full history, and if you picked the wrong version you copy another one. Removing a marker by version ID is equally valid when it is done carefully, but it uses the same permission and the same action that permanently deletes real data. Mistype the version ID and you delete the version you were trying to save. If you do use it, first confirm in the version list that the ID you are about to delete sits under DeleteMarkers, not Versions. The console cannot copy a version in place, so the copy is a CLI or SDK task.
The decision path from a 404 to a restored object looks like this:
The first branch matters most. If the bucket never had versioning, a delete leaves no earlier versions in S3. The only way back is then a copy held somewhere else, such as a backup or a replicated bucket.
#Example: reading the version history of one web page
The AWS CLI reference shows a listing for a key called index.html in a bucket named amzn-s3-demo-bucket. It is worth reading slowly, because a real accidental delete produces output of the same shape. The command is:
1aws s3api list-object-versions \
2 --bucket amzn-s3-demo-bucket \
3 --prefix index.htmlThe output, with the owner, ETag and storage class fields left out, is:
1{
2 "DeleteMarkers": [
3 { "IsLatest": true, "VersionId": "B2VsEK5saUNNHKcOAJj7hIE86RozToyq", "Key": "index.html", "LastModified": "2015-11-10T00:57:03.000Z" },
4 { "IsLatest": false, "VersionId": ".FLQEZscLIcfxSq.jsFJ.szUkmng2Yw6", "Key": "index.html", "LastModified": "2015-11-09T23:32:20.000Z" }
5 ],
6 "Versions": [
7 { "LastModified": "2015-11-10T00:20:11.000Z", "VersionId": "Rb_l2T8UHDkFEwCgJjhlgPOZC0qJ.vpD", "Key": "index.html", "IsLatest": false, "Size": 38 },
8 { "LastModified": "2015-11-09T23:26:41.000Z", "VersionId": "rasWWGpgk9E4s0LyTJgusGeRQKLVIAFf", "Key": "index.html", "IsLatest": false, "Size": 38 },
9 { "LastModified": "2015-11-09T22:50:50.000Z", "VersionId": "null", "Key": "index.html", "IsLatest": false, "Size": 533823 }
10 ]
11}Notice first that delete markers and data versions come back in two separate lists. To see the order of events, put every entry on one timeline by LastModified. The null version, 533,823 bytes, was written at 22:50:50; a version ID of null marks an object stored before versioning was turned on. A 38-byte version followed at 23:26:41. At 23:32:20 someone deleted the page, which added the first marker. At 00:20:11 a new 38-byte version was uploaded. At 00:57:03 the page was deleted again, and that second marker has IsLatest: true. That marker is why the page returns 404 now.
The version to restore is the one that was current immediately before the latest marker: Rb_l2T8UHDkFEwCgJjhlgPOZC0qJ.vpD, written at 00:20:11. Copying it back over the same key would look like this:
1aws s3api copy-object \
2 --bucket amzn-s3-demo-bucket \
3 --key index.html \
4 --copy-source "amzn-s3-demo-bucket/index.html?versionId=Rb_l2T8UHDkFEwCgJjhlgPOZC0qJ.vpD"The response carries a new VersionId for the copy and a CopySourceVersionId naming the version it came from. Nothing in the listing above disappears. One more detail matters: --prefix matches every key that begins with the text, so a key such as index.html.bak would appear in the same output. Always check the Key field of the entry you pick. If a key contains spaces or other special characters, URL-encode it in --copy-source, as the CLI reference requires.
#Your turn: delete and bring back a test file
The objective is to create two versions of a small test object, delete it with a simple delete, and restore the second version by copying it. Replace your-lab-bucket with your bucket name throughout. Write down each version ID as it appears; you will need them for the checks.
- Confirm versioning is on:
aws s3api get-bucket-versioning --bucket your-lab-bucket. The output should be{ "Status": "Enabled" }. IfStatusis missing or saysSuspended, stop here and use another bucket, because the rest of the lab depends on deletes adding markers. - Create the first version:
echo "version one" > notes.txt, thenaws s3api put-object --bucket your-lab-bucket --key kby-lab/notes.txt --body notes.txt. Record theVersionIdin the response as version one. - Create the second version:
echo "version two" > notes.txtand run the sameput-objectcommand. Record the newVersionIdas version two. - Delete the object with a simple delete:
aws s3api delete-object --bucket your-lab-bucket --key kby-lab/notes.txt. Do not add--version-id. The response should show"DeleteMarker": trueand the version ID of the new marker. - Prove it looks deleted:
aws s3api head-object --bucket your-lab-bucket --key kby-lab/notes.txtshould fail with a 404 Not Found. If you lacks3:ListBucket, S3 returns 403 Forbidden instead. - List the history:
aws s3api list-object-versions --bucket your-lab-bucket --prefix kby-lab/notes.txt. Expect one entry underDeleteMarkerswithIsLatest: trueand two underVersions, bothIsLatest: false, whose IDs match versions one and two. - Restore version two by copying it over the key:
aws s3api copy-object --bucket your-lab-bucket --key kby-lab/notes.txt --copy-source "your-lab-bucket/kby-lab/notes.txt?versionId=VERSION_TWO_ID". The response shows a newVersionIdandCopySourceVersionIdequal to version two.
Following along? Tap a step’s number to tick it off.
The pass condition is that the key reads again and returns the text version two, while the listing still holds both original versions and the marker. Stop if any command returns AccessDenied: that is a permissions gap to fix with whoever owns the bucket, not something to work around with broader rights. To roll the restore back, run the simple delete from step 4 again. It adds a fresh delete marker on top, returning the key to its deleted state without removing any data. For cleanup, leave the lab key deleted with a simple delete. Its versions stay in the bucket and are charged as stored objects until a lifecycle rule or the bucket owner removes them, so tell the owner the kby-lab/ prefix exists.

#Did it work? Reading the evidence
Three checks prove the restore, and each answers a different question. First, aws s3api head-object --bucket your-lab-bucket --key kby-lab/notes.txt should now succeed, and its VersionId should equal the version ID returned by copy-object. That shows a plain read reaches the restored copy, not the marker.
Second, aws s3api get-object --bucket your-lab-bucket --key kby-lab/notes.txt restored.txt downloads the current version to a local file. Open restored.txt: it should say version two. That shows you restored the right content, not merely some version. Third, run list-object-versions again. The copy should be the only entry with IsLatest: true, and versions one and two and the delete marker should all still be listed as noncurrent. That shows the restore removed nothing, which is the whole reason for choosing the copy.
If head-object still returns 404, look at the listing before trying anything else. A newer delete marker on top means someone or something deleted the key again after your copy.
#Mistakes that make a recovery fail
- Passing a version ID to a delete by habit. A delete with
--version-idpermanently removes whatever version you name. Aimed at a data version instead of the marker, it destroys the copy you came to save. - Deleting the marker without its version ID. A simple delete on a key whose current version is already a marker adds another marker. The object stays hidden and the history gets longer.
- Picking the version by its position in the output. Delete markers and versions are listed separately, so the first data version shown is not always the one you want. Sort by
LastModifiedand check theKey. - Missing
s3:GetObjectVersion. Copying a named version needs it, as well ass3:PutObjecton the destination. A role that can read current objects may still get AccessDenied here; our guide to fixing S3 AccessDenied when an EC2 instance role reads a bucket walks through tracing that kind of gap. - Forgetting the KMS key. If the object is encrypted with a customer managed KMS key, the copy also needs
kms:Decryptto read the source. Encrypting the new version with a customer managed key needskms:GenerateDataKeyas well. - Trying to copy a very large object in one call.
copy-objectaccepts a source of up to 5 GB. A larger object needs a different restore method, which is worth agreeing with the bucket owner before you start. - Treating an overwrite as a loss. In a versioned bucket an overwrite creates a new version, and the old one is still listed. If your bucket is filled by an automated copy, such as an Amazon AppFlow flow from SharePoint Online, a file that “changed overnight” can be rolled back the same way.
#Key takeaways
- In a versioned bucket, a plain delete hides an object behind a delete marker; it does not erase it.
list-object-versionsis the only way to see delete markers, and you read it by timestamp, not by position.- Copying the last good version over the key restores the object and keeps every version, so the restore itself can be undone.
- Any delete that names a version ID, and any noncurrent-version expiration rule, removes data for good.
- Prove a restore with three things: the new version ID on
head-object, the right content fromget-object, and an unchanged history.
#Recovering objects in a bucket other people rely on
Before restoring anything in production, run two read-only checks. get-bucket-versioning tells you whether versions are kept at all and whether MFA delete is enabled. list-object-versions for the exact key tells you whether a good version is still there. If MFA delete is on, permanently deleting a version or changing the versioning state needs a code from the account’s MFA device. That is a deliberate brake on exactly the risky actions above. It can only be enabled with the CLI or API, not in the console.
Permissions are the main control. The restore described here needs s3:ListBucketVersions, s3:GetObjectVersion and s3:PutObject. It does not need s3:DeleteObjectVersion, which is the permission that can destroy history, so keep that one with very few people. AWS also notes that to stop users removing objects you must deny s3:DeleteObject, s3:DeleteObjectVersion and s3:PutLifeCycleConfiguration, because a lifecycle rule can delete on someone’s behalf. Check the bucket’s lifecycle configuration for a NoncurrentVersionExpiration action, since that sets how long an accidentally deleted object can still be recovered.
The rollback boundary is simple. A restore by copy can be reversed with a simple delete, and nothing is lost either way. Anything that removes a version by ID cannot be reversed, so it needs a second person to check the version ID before it runs. Sometimes the version you need is no longer listed, or the object is too large for a single copy. Then stop and hand the case to whoever owns backups for that data. The next safe decision after any restore is to find out why the delete happened, whether a person, a script or a lifecycle rule, before it happens again.
About this page
Credited to
Dates
Published
Sources
Corrections
No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.
Recommended reading
More from KBY that goes with this article.
Small business ITBackups a small business can actually restore from: three copies, one offline, and a test
5 min read


Related articles
Cloud Infrastructure and Operations
AWS VPC Instance Has No Internet Access: Check Route Table and NAT
Why an EC2 instance in a VPC has no internet access, and how to trace the route table, NAT gateway and internet gateway to find the missing link safely.
ReadSystems Engineering
Find Stale Computer Accounts in Active Directory with PowerShell, and Disable Them Before You Delete
List computer accounts that have not logged on for 90 days, record them, disable and quarantine them in stages, and keep a clear way back.
ReadSecurity & Operations
Audit and Tighten Microsoft Defender Antivirus Exclusions Across Intune, Group Policy and Local Lists
Find every Defender Antivirus exclusion, replace the risky broad ones with narrow entries, remove them where they were set, and stop local additions coming back.
ReadDiscover more
Graduate Learning
- TutorialCopying SharePoint Online Document Libraries to Amazon S3 with Amazon AppFlow
- TutorialIncreasing an AWS EBS Volume Size and Extending the Linux File System
- TutorialUsing AWS CloudTrail Event History to Find Who Changed a Security Group
- TutorialAWS VPC Instance Has No Internet Access: Check Route Table and NAT
Lexicon Definitions
Was this useful?
Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.
Build practical engineering skills.
Receive new lessons, learning paths, practical exercises and early-career guidance.
Comments
Add a thoughtful note on Recovering a Deleted Amazon S3 Object from a Versioned Bucket. Comments are checked for spam and held for moderation before appearing.