AWS S3 Bucket Versioning: Protect Your Data from Accidental Deletes and Overwrites

A hands-on guide to enabling S3 versioning, recovering deleted files, and keeping costs under control with lifecycle rules." tags: aws, s3, cloud, beginners Overwriting the wrong file or deleting an important object in S3 is easy to do, and by default it is permanent. S3 Versioning fixes this. It keeps every version of every object in your bucket, so you can recover from mistakes and from application failures. In this post I enable versioning, test it, and recover a deleted file. vishalcloudjourney ???? What is S3 Versioning? Versioning is a bucket-level feature that keeps multiple variants of an object in the same bucket. Every upload of the same key creates a new version with its own unique Version ID. A bucket is always in one of three states: Unversioned (the default) Versioning-enabled Versioning-suspended ⚠️ Once you enable versioning, you cannot go back to unversioned. You can only suspend it, and existing versions are kept. ✅

AWS S3 Bucket Versioning: Protect Your Data from Accidental Deletes and Overwrites

A hands-on guide to enabling S3 versioning, recovering deleted files, and keeping costs under control with lifecycle rules."

tags: aws, s3, cloud, beginners

Overwriting the wrong file or deleting an important object in S3 is easy to do, and by default it is permanent. S3 Versioning fixes this. It keeps every version of every object in your bucket, so you can recover from mistakes and from application failures. In this post I enable versioning, test it, and recover a deleted file.

vishalcloudjourney

???? What is S3 Versioning?

Versioning is a bucket-level feature that keeps multiple variants of an object in the same bucket. Every upload of the same key creates a new version with its own unique Version ID.

A bucket is always in one of three states:

  • Unversioned (the default)
  • Versioning-enabled
  • Versioning-suspended

⚠️ Once you enable versioning, you cannot go back to unversioned. You can only suspend it, and existing versions are kept.

✅ Why use it?

  • Recover from accidental deletion: a delete only adds a delete marker, and the data stays.
  • Recover from accidental overwrites: older versions are still there.
  • Keep a history: useful for audits and rollbacks.
  • Safer backups: a good protection layer against human error and some application bugs.

????️ Hands-on: Enable versioning in the console

Step 1: Create a bucket

  1. Open the S3 console and click Create bucket.
  2. Enter a globally unique bucket name, such as vishal-versioning-demo-123.
  3. Choose your region and keep Block all public access on.
  4. Click Create bucket.

Step 2: Enable versioning

  1. Open your bucket and go to the Properties tab.
  2. Find Bucket Versioning and click Edit.
  3. Select Enable and click Save changes.

Step 3: Upload and overwrite a file

  1. Create a file demo.txt containing Version 1 and upload it.
  2. Change the content to Version 2 and upload it again with the same name.
  3. Open the Objects tab and turn on the List versions toggle.

You will see two versions of demo.txt, each with its own Version ID. The latest one is the current version, and the other is a noncurrent version.

Step 4: Delete and recover

  1. Turn List versions off, select demo.txt and click Delete.
  2. The file seems to disappear. Turn List versions back on.
  3. You will see a Delete marker on top of your versions. Your data is safe.
  4. Select the delete marker and delete it. demo.txt comes back.

To remove a version permanently, delete it by its specific Version ID.

⌨️ Hands-on: Using the AWS CLI

# Enable versioning
aws s3api put-bucket-versioning \
  --bucket vishal-versioning-demo-123 \
  --versioning-configuration Status=Enabled

# Check the status
aws s3api get-bucket-versioning --bucket vishal-versioning-demo-123

# List all versions and delete markers
aws s3api list-object-versions --bucket vishal-versioning-demo-123

# Download a specific version
aws s3api get-object \
  --bucket vishal-versioning-demo-123 \
  --key demo.txt \
  --version-id  demo-old.txt

# Permanently delete one specific version
aws s3api delete-object \
  --bucket vishal-versioning-demo-123 \
  --key demo.txt \
  --version-id 

???? Things to know

  • Cost: every version is stored and billed as a separate object, so storage grows with each update.
  • Lifecycle rules: use them to expire noncurrent versions after N days, or move them to a cheaper storage class. This keeps costs under control.
  • Objects uploaded before versioning: they get the Version ID null.
  • MFA Delete: an optional extra protection that requires MFA to permanently delete a version or change the versioning state. Only the bucket owner's root account can enable it, and only through the CLI or API.
  • Replication: Cross-Region Replication requires versioning on both buckets.

???? Best practices

  1. Turn versioning on for buckets that hold important or hard-to-recreate data.
  2. Add a lifecycle rule to clean up old noncurrent versions.
  3. Restrict who can permanently delete versions with IAM and bucket policies.
  4. Consider MFA Delete for critical buckets.
  5. Monitor storage growth in the S3 console and in billing.

???? Cleanup

A versioned bucket is not empty until all versions and delete markers are removed. Use Empty in the console, then delete the bucket, so you don't pay for leftover data.

???? Key learnings

  • Versioning turns a permanent delete into a recoverable one.
  • A delete on a versioned bucket adds a delete marker, and the data stays in the bucket.
  • Lifecycle rules are the best way to keep versioning affordable.

???? Connect with me

If this helped you, drop a ❤️ and tell me how you use S3 versioning in the comments!

vishalcloudjourney #AWS #S3 #CloudStorage #DataProtection

Share