S3 Versioning
Versioning keeps every version of an object so you can recover from accidental overwrites and deletes.
Versioning makes a bucket keep a full history of each object. When you overwrite or delete an object, the old version is retained instead of being lost.
How it behaves
- Each version gets a unique version ID.
- Deleting an object adds a delete marker rather than removing data; you can restore the previous version.
- Once enabled, versioning can be suspended but not fully turned off.
Why it matters
Versioning protects against human error and ransomware. Combined with MFA Delete, it makes destroying data require a second factor. Remember that keeping many versions increases storage cost — pair it with a lifecycle rule to expire old versions.
Example
# Turn on versioning for a bucket
aws s3api put-bucket-versioning \
--bucket my-app-assets \
--versioning-configuration Status=Enabled
# List all versions of the objects
aws s3api list-object-versions --bucket my-app-assets \
--query "Versions[].[Key,VersionId,IsLatest]" --output tableWhen to use it
- A developer accidentally overwrites a critical config file in S3 and recovers the previous version within minutes using S3 versioning.
- A compliance team enables versioning on an S3 bucket so every change to legal documents is preserved and traceable for audits.
- A CI/CD pipeline uploads build artifacts to a versioned bucket, allowing rollback to any prior build artifact by referencing its version ID.
More examples
Enable Versioning on a Bucket
Turns on versioning for a bucket so every PUT or DELETE creates a new version rather than overwriting the object.
aws s3api put-bucket-versioning \
--bucket my-bucket \
--versioning-configuration Status=EnabledList All Versions of an Object
Lists every stored version of a specific object with timestamps, so you can identify which version to restore.
aws s3api list-object-versions \
--bucket my-bucket \
--prefix config/app.json \
--query 'Versions[].{VersionId:VersionId,Modified:LastModified,Latest:IsLatest}' \
--output tableRestore a Previous Version
Restores an older object version by copying it over the current one, effectively rolling back without deleting history.
# Copy the old version back as the current version
aws s3api copy-object \
--bucket my-bucket \
--copy-source my-bucket/config/app.json?versionId=abc123xyz \
--key config/app.json
Discussion