Sub topic 11
Versioning and recovering from mistakes
This chapter assumes you can create a bucket, upload objects with the AWS CLI, and read basic IAM policies. Versioning is a bucket setting that keeps every version of an object instead of overwriting it. It is the main protection against accidental overwrites and deletes.
How versioning works
A bucket is in one of three states: unversioned (the default), versioning-enabled, or versioning-suspended. Once you enable versioning, you can only suspend it. You cannot return the bucket to unversioned.
With versioning on, S3 gives each upload a unique version ID. A new upload under an existing key does not replace the old data. It becomes the current version, and the older ones stay stored. Objects written before you enabled versioning keep the version ID null.
A delete without a version ID does not remove data. S3 adds a delete marker, which is a zero-byte placeholder that becomes the current version. A plain GET on that key returns 404, but the earlier versions still exist.
Enable versioning and inspect versions
aws s3api put-bucket-versioning \
--bucket my-example-bucket-123456789012 \
--versioning-configuration Status=Enabled
aws s3api list-object-versions \
--bucket my-example-bucket-123456789012 \
--prefix reports/The output lists Versions and DeleteMarkers. Each entry shows the Key, VersionId, and IsLatest.
Recovering from mistakes
Which recovery you use depends on what went wrong.
| Mistake | Recovery |
|---|---|
| Object deleted with a plain delete | Delete the delete marker by its version ID. The previous version becomes current. |
| Object overwritten with bad content | Copy an older version onto the same key, which creates a new current version. Or delete the bad version by its ID. |
| Need an old version without changing the current one | GET it with `--version-id`. |
The next commands show each recovery. Replace the version IDs with values from list-object-versions.
# Undo a plain delete: remove the delete marker
aws s3api delete-object \
--bucket my-example-bucket-123456789012 \
--key reports/2026/report.csv \
--version-id MARKER_VERSION_ID
# Restore an old version as the new current version
aws s3api copy-object \
--bucket my-example-bucket-123456789012 \
--key reports/2026/report.csv \
--copy-source "my-example-bucket-123456789012/reports/2026/report.csv?versionId=OLD_VERSION_ID"
# Download an old version without changing anything
aws s3api get-object \
--bucket my-example-bucket-123456789012 \
--key reports/2026/report.csv \
--version-id OLD_VERSION_ID \
old-report.csvInteractions with other features
- Cost: every stored version is billed as an object. Frequent overwrites of large files can multiply your storage bill.
- Lifecycle rules: use
NoncurrentVersionExpirationto delete old versions after a set number of days. UseNoncurrentVersionTransitionsto move them to cheaper classes. AddExpiredObjectDeleteMarkercleanup so orphaned markers do not accumulate. A plainExpirationrule on a versioned bucket adds a delete marker instead of freeing space. - Replication: S3 replication requires versioning on both the source and destination buckets.
- Suspending: suspending stops new version IDs, since new writes get the ID
null. Existing versions stay and keep costing money.
Exercise
In a versioned bucket, you run aws s3 rm s3://bucket/data.csv. A GET for the key now returns 404. Is the data gone, and how do you bring it back?
Show answer
The data is not gone. A plain delete adds a delete marker as the current version, and earlier versions remain. Run list-object-versions to find the delete marker's VersionId, then delete the marker with delete-object --version-id <marker id>. The previous version becomes current again.
Exercise
Your versioned bucket's storage cost keeps rising although the number of keys is stable. What is the likely cause, and what lifecycle setting addresses it?
Show answer
Noncurrent versions from repeated overwrites are accumulating, and each is billed. Add a lifecycle rule with NoncurrentVersionExpiration (and optionally NoncurrentVersionTransitions to cheaper classes) to remove or archive old versions.