S3 Versioning

Difficulty: Beginner

Question

What is S3 Versioning, how does it protect against deletes and overwrites, and what does it cost you?

Answer

S3 Versioning keeps every version of every object in a bucket. When you overwrite an object, S3 stores the new data as a new version and keeps the old one; when you delete an object without specifying a version, S3 adds a delete marker instead of removing data. So accidental overwrites, deletes, and even many ransomware-style attacks become recoverable. The trade-off is storage cost, because old versions are billed until you expire them.

Bucket versioning states

A bucket is in one of three states:

  • Unversioned (default): overwrites and deletes are permanent.
  • Enabled: every write gets a unique version ID.
  • Suspended: new writes get the version ID null and overwrite each other, but existing versions are kept.

Once enabled, a bucket can never go back to unversioned, only to suspended.

What actually happens on each operation

OperationWithout versioningWith versioning
PUT an existing keyOld data is goneNew current version; old one becomes noncurrent
DELETE keyObject is goneDelete marker becomes current; data remains
GET key after delete404404, because the current version is a delete marker
DELETE key?versionId=...n/aThat specific version is permanently removed
RestoreNot possibleDelete the delete marker, or copy an old version on top

Controlling the cost

Every version is a full object billed at its storage class rate. A 100 MB file overwritten daily keeps growing your bill. Fix it with lifecycle rules:

  • NoncurrentVersionExpiration with NoncurrentDays to delete old versions after, say, 30 days.
  • NewerNoncurrentVersions to keep, for example, the last 5 versions regardless of age.
  • NoncurrentVersionTransition to move old versions to cheaper classes like Glacier.
  • ExpiredObjectDeleteMarker to clean up delete markers that no longer hide any versions.

Related features that depend on versioning

  • Replication (Same-Region and Cross-Region) requires versioning on both source and destination buckets.
  • S3 Object Lock (WORM retention for compliance, such as keeping financial records for years) requires versioning.
  • MFA Delete requires the root user's MFA to permanently delete a version or change the versioning state. It can only be enabled by the root user through the CLI or API.

Gotchas

  • A DELETE in a versioned bucket does not free space; the data is still there as a noncurrent version.
  • Listing with ListObjectsV2 does not show old versions; use ListObjectVersions.
  • Versioning protects data, not access. A role with s3:DeleteObjectVersion can still destroy history, so combine it with least privilege, Object Lock, or MFA Delete for critical buckets like backups.
Versions of one object over time. Each write adds a version, and a plain delete only adds a marker on top.
  1. PUT report.pdf creates version v1: v1 is the current version.
  2. PUT report.pdf again creates v2: v2 is current, v1 is now noncurrent.
  3. DELETE report.pdf adds a delete marker: GET returns 404, but v1 and v2 still exist.
  4. Remove the delete marker: v2 becomes current again: file restored.
  5. Lifecycle expires noncurrent v1: Old versions are cleaned up after 30 days.

Code examples

Enable versioning, recover a deleted file, and expire old versions

aws s3api put-bucket-versioning --bucket acme-docs \
  --versioning-configuration Status=Enabled

# See all versions and delete markers for a key
aws s3api list-object-versions --bucket acme-docs --prefix report.pdf

# Undo an accidental delete by removing the delete marker
aws s3api delete-object --bucket acme-docs --key report.pdf \
  --version-id <delete-marker-version-id>

# Keep 3 old versions, expire anything noncurrent for 30+ days
aws s3api put-bucket-lifecycle-configuration --bucket acme-docs \
  --lifecycle-configuration '{"Rules":[{"ID":"trim-versions","Status":"Enabled",
  "Filter":{},"NoncurrentVersionExpiration":{"NoncurrentDays":30,"NewerNoncurrentVersions":3}}]}'

Deleting the delete marker by its version ID brings the previous version back as current. The lifecycle rule stops version history from growing forever.

Key points

  • Versioning keeps all versions; overwrites create new versions and deletes add delete markers
  • Once enabled, a bucket can only be suspended, never returned to unversioned
  • Every version is billed; use lifecycle rules on noncurrent versions
  • Replication and Object Lock require versioning
  • MFA Delete needs the root user and the CLI/API to enable

Common follow-up questions

  • How would you restore an object that was deleted yesterday in a versioned bucket?
  • What is the difference between suspending versioning and disabling it?
  • How do S3 Object Lock governance and compliance modes differ?
  • How would you stop versioning from blowing up your storage bill?

Interview tips

  • Explain delete markers clearly; it is the part most candidates get wrong
  • Bring up the cost side and lifecycle rules unprompted
  • Connect versioning to replication and Object Lock to show you see the bigger picture

Concepts covered

S3 Versioning, Delete Markers, Lifecycle Rules, MFA Delete, Object Lock

More AWS & Cloud Fundamentals questions