Operations

How to recover deleted or overwritten files in Amazon S3

How S3 Versioning changes a delete or overwrite into a recoverable event, with safe console and CLI steps, lifecycle controls, and the limits to know before an incident.

Founder, developer
September 23, 202610 min read
The short versionPlain termsFor builders

Check whether Versioning was active when the change happened. Remove the current delete marker to reveal a deleted file, or copy a known older version onto the same key to reverse an overwrite while preserving history. Use CloudTrail data events to find the version ID, the AWS rollback Guidance when millions of objects are involved, and a recycle bin so the people who use the files can undo their own deletes. Then bound the recovery window with a reviewed noncurrent-version lifecycle rule.

A file disappears from a shared prefix, or yesterday's spreadsheet is replaced by the wrong copy. The first question is not which recovery command to run. It is whether S3 Versioning was enabled on the bucket when the change happened.

When Versioning is enabled, an ordinary delete usually adds a delete marker and an overwrite creates a new current version. The older bytes remain available by version ID. This guide shows how to tell those cases apart, recover without throwing away more history, find the right version ID when the history is long, roll back millions of objects at once, and put a limit on the storage that old versions consume.

Start by checking the bucket and the object history

Open the bucket's Properties tab in the S3 console and check Bucket Versioning. If it says Enabled, switch on Show versions in the Objects view and search for the exact key. A deleted object should appear with one or more delete markers alongside its data versions. An overwritten object should show several data versions with different version IDs and modified times.

Versioning protects only writes and deletes that happen after it is enabled. If the bucket was unversioned when the only copy was deleted or replaced, turning Versioning on now cannot reconstruct it. Check any replica bucket, backup, application export or other copy before concluding that the file is gone.

A Suspended bucket needs extra care. Versions created while Versioning was enabled still keep their IDs, but new writes use a null version ID. Record what the console shows before changing anything, especially if the key has both numbered and null versions.

aws s3api get-bucket-versioning --bucket amzn-s3-demo-bucket

aws s3api list-object-versions \
  --bucket amzn-s3-demo-bucket \
  --prefix 'finance/forecast.xlsx'

Recover a file hidden by a delete marker

In a versioned bucket, a simple DeleteObject request does not normally erase the existing data version. S3 creates a delete marker and makes that marker current, so an ordinary GetObject request behaves as though the key is missing.

In the console, turn on Show versions, select the current delete marker for the exact key, choose Delete, and confirm permanent deletion of that marker. The most recent data version below it becomes current again. Do not select a data version unless you intend to remove that version permanently.

The CLI operation must include the delete marker's version ID. Running delete-object without --version-id creates another delete marker instead of revealing the file. Copy the ID from list-object-versions and check the key again after the command completes.

aws s3api delete-object \
  --bucket amzn-s3-demo-bucket \
  --key 'finance/forecast.xlsx' \
  --version-id 'delete-marker-version-id'

Roll back an overwrite without deleting later history

An overwrite is different: the wrong upload is a real current data version, with the earlier version still underneath it. You can make the older content current by permanently deleting every newer version, but that removes useful evidence and makes a second mistake harder to undo.

AWS recommends copying the chosen older version back onto the same key. CopyObject reads the source by version ID and writes a new current version. The mistaken upload, the recovered copy and the earlier original all remain in the history. Confirm the older version by modified time, size and, where available, checksum before copying it. For long documents, asking a model what changed can speed up that check; this guide to Bedrock models for document questions compares the options.

The role doing the recovery needs permission to read the chosen version and write the destination key. A KMS-encrypted object can also require permission to use its KMS key. Test the command on one key and record the source version ID in the incident or change record.

aws s3api copy-object \
  --bucket amzn-s3-demo-bucket \
  --key 'finance/forecast.xlsx' \
  --copy-source 'amzn-s3-demo-bucket/finance/forecast.xlsx?versionId=older-version-id'

Find the right version with CloudTrail and Athena

A restore needs the version ID of the last good state, or trial and error risks a second loss. AWS CloudTrail records object-level calls such as DeleteObject and PutObject as data events, and each event names the version ID it created, so the event at the time of the incident points at the version to go back to.

Data events are off by default and are billed separately. Turn them on for the bucket in a trail or a CloudTrail Lake event data store before the incident, because CloudTrail Event history, the free 90-day view that is on by default, shows bucket-level management events only. A trail delivers its logs to an S3 bucket, where Amazon Athena queries them and a lifecycle rule sets how long they stay. CloudTrail Lake keeps events for up to seven years under its seven-year retention pricing, or about ten years under one-year extendable retention pricing. Both take SQL queries, so you can filter millions of events down to the one object and the one minute that matter.

Know which actions are permanent

Deleting a specific data version by version ID permanently removes that version. S3 Versioning does not provide another recovery layer beneath it. The same is true when a lifecycle rule expires a noncurrent version. Treat both operations as disposal, with narrower permissions and a longer review path than an ordinary delete.

S3 Object Lock is the AWS control for versions that must resist permanent deletion for a retention period or legal hold. It requires Versioning and protects individual object versions. It does not stop someone from uploading a new version or placing a delete marker above a protected version, so recovery procedures still need to include Show versions and version IDs.

Keep enough history without keeping every version forever

Each stored version is a complete object and is billed as one. Replacing a 2 GB presentation ten times can leave roughly 20 GB of stored versions, even though normal listing shows one current key. Versioning needs a recovery window and a matching lifecycle rule, not a vague promise to clean up later.

Use NoncurrentVersionExpiration to remove noncurrent versions after the recovery period your business has chosen. A rule can also retain a number of newer noncurrent versions, but AWS requires a noncurrent-days setting when that option is used. Review Object Lock retention before applying expiration, because a protected version remains protected until its retention or legal hold permits deletion.

  • Choose the recovery window from the time it usually takes your team to notice a bad delete or overwrite.
  • Estimate storage from the size and replacement rate of the files, not only the size of the current objects.
  • Test the lifecycle rule on a narrow prefix before applying it to the whole bucket.
  • Monitor noncurrent version storage separately so a busy prefix does not become a surprise bill.

Restore millions of objects at once

A deployment script that overwrites or deletes millions of files is too big to fix one key at a time. The AWS Guidance for rolling back changes to datasets in Amazon S3, deployed as a CloudFormation stack, inventories the bucket, uses Athena queries to work out which version each key should have as of a point in time, and runs the restores through S3 Batch Operations.

Open-source scripts exist too, but check their maintenance state before running one against production. The AWS sample repository for restoring deleted objects, aws-samples/restore-s3-deleted-objects, is archived, was last updated in May 2024, and has 5 stars and 7 forks. Treat scripts like it as a template to test in a sandbox, not as a finished tool.

Changes to revertBucket sizeTime to restore
A few thousandUp to millions of objectsUnder 15 minutes end to end
1 million10 billion objectsUnder 1 hour
100 million10 billion objectsUnder 15 hours

Decide between a manual fix and automation

A misconfigured lifecycle rule has just expired ten thousand files from a production bucket. The mechanics of a delete marker matter less now than the speed of the response, and that depends on how many objects are involved and who is available to run the restore. Five checks settle whether one person fixes it by hand or the team deploys a rollback.

  • Count the objects: how many keys across which prefixes need restoring.
  • Check the depth: whether the version you want still exists under the newest marker.
  • Set the clock: how long the applications that read these files can wait.
  • Match the skill: whether someone can run batch CLI operations against production without error.
  • Compare the cost: the price of a rollback tool against the hours of engineering time a manual restore takes.
The solution

Separate everyday file access from recovery administration

Most people need the current file, not permission to permanently delete versions. Keep version cleanup and lifecycle administration with a small AWS operations group, use CloudTrail data events where object-level history is required, and give the wider team a browser view of the bucket, connected through an IAM role in your account, with a recycle bin that lists the files deleted in the last 30 days. On a versioned bucket a restore from that bin deletes the current delete marker, so the previous version is current again and nothing is removed for good. Copying an older version over an overwrite stays a console or CLI task.

  1. Connect the bucket

    a CloudFormation stack in your account creates the scoped IAM role, and BucketDesk signs in through that role.

  2. Open the recycle bin

    administrators and operators see every file deleted in the last 30 days across the connected buckets, with the time of each delete.

  3. Restore the file

    on a versioned bucket BucketDesk deletes the current delete marker; on a bucket without versioning it moves the file back from a trash folder inside the bucket.

Try it out now

The recycle bin is included on Ultra, $350 a month or $3,500 a year, and is available as an add-on on Standard.

Primary sources

Discussion

0 comments · open to guests · moderated
Comments appear after a quick review.

Liked this? Get the next article by email. No schedule, no digest, one click to leave.

Keep reading

All articles →
Let's chat

Questions? Message directly.