Difficulty: Beginner
What is S3 Versioning, how does it protect against deletes and overwrites, and what does it cost you?
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.
A bucket is in one of three states:
null and overwrite each other, but existing versions are kept.Once enabled, a bucket can never go back to unversioned, only to suspended.
| Operation | Without versioning | With versioning |
|---|---|---|
PUT an existing key | Old data is gone | New current version; old one becomes noncurrent |
DELETE key | Object is gone | Delete marker becomes current; data remains |
GET key after delete | 404 | 404, because the current version is a delete marker |
DELETE key?versionId=... | n/a | That specific version is permanently removed |
| Restore | Not possible | Delete the delete marker, or copy an old version on top |
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:
NoncurrentDays to delete old versions after, say, 30 days.DELETE in a versioned bucket does not free space; the data is still there as a noncurrent version.ListObjectsV2 does not show old versions; use ListObjectVersions.s3:DeleteObjectVersion can still destroy history, so combine it with least privilege, Object Lock, or MFA Delete for critical buckets like backups.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.
S3 Versioning, Delete Markers, Lifecycle Rules, MFA Delete, Object Lock