Enable Versioning to Undo Deletes
Without versioning, an overwrite or rm is permanent and a bad deploy that rewrites every object is unrecoverable. Versioning keeps prior versions so a delete becomes a soft, reversible event — provided you cap how long noncurrent versions live.
Procedure
- Turn versioning on before you need it:
aws s3api put-bucket-versioning --bucket acme --versioning-configuration Status=Enabled. - Immediately add a
NoncurrentVersionExpirationrule so old versions do not accumulate forever:{"NoncurrentVersionExpiration":{"NoncurrentDays":30}}. - For databases and filesystems, use the native equivalent: WAL + PITR for Postgres (
recovery_target_time), ZFS/Btrfs snapshots for files. - Understand that a delete writes a delete-marker; the data is still there and recoverable by removing the marker:
aws s3api delete-object --bucket acme --key path --version-id <delete-marker-id>. - Recover a specific version:
aws s3api list-object-versions --bucket acme --prefix paththenget-object --version-id. - Add MFA-delete or a bucket policy denying
DeleteObjectVersionoutside a break-glass role, so a compromise or script cannot purge history. - Test the recovery: delete a test object, confirm the current listing hides it, restore the prior version, confirm content matches.
- Re-baseline storage cost after enabling; versioning multiplies stored bytes by the change rate within the retention window.
Pitfalls
- Enabling versioning with no noncurrent expiry, so storage grows without bound and the cost alarm fires months later.
- Believing versioning protects against a malicious actor who has
s3:DeleteObjectVersion— it does not; lock the permission. - A lifecycle rule that deletes noncurrent versions after 1 day, which is too short to catch a slow-failing deploy's overwrites.
- Confusing a delete-marker with the absence of the object;
list-objectshides it butlist-object-versionsstill shows the data. - Assuming versioning replicates to a bucket-replication target automatically; the target's versioning must be enabled too.
- Enabling versioning on a bucket with a huge existing object count and no plan for the first-month cost of retained versions.
- Forgetting that versioning changes ETag semantics and some SDK "overwrite" calls now leave the old bytes billed.
Verification
aws s3api get-bucket-versioning --bucket acme # Status: Enabled
aws s3api list-object-versions --bucket acme --prefix test/key \
--query 'Versions[].VersionId' --output text | wc -w # >1 after edits
Versioning is Enabled, a prior version is retrievable by --version-id, and a NoncurrentVersionExpiration rule bounds retention.
Report: "Versioning enabled on with 30d noncurrent expiry; recovered test/key version ; delete now reversible within 30 days."