🧩 How it works
When S3 versioning is enabled, each update to an object creates a new version:
- The latest version = current version
- Older ones = noncurrent versions
Lifecycle rules can:
- Delete noncurrent versions older than X days (
NoncurrentDays), and - Retain only the N most recent noncurrent versions (
NewerNoncurrentVersions).
So to keep two versions total, we’ll tell S3 to keep:
- 1 current version
- 1 noncurrent version (delete anything older)
⚙️ Lifecycle configuration example (JSON)
{
"Rules": [
{
"ID": "Keep two latest versions",
"Status": "Enabled",
"Filter": {},
"NoncurrentVersionExpiration": {
"NoncurrentDays": 1,
"NewerNoncurrentVersions": 1
}
}
]
}
💡 Meaning
| Field | Description |
|---|---|
NoncurrentDays: 1 |
Wait one day after a version becomes noncurrent before evaluating it for deletion. |
NewerNoncurrentVersions: 1 |
Retain only one noncurrent version (so total of two versions: current + 1 previous). |
🧾 Apply it to your bucket
Save the JSON above to a file, e.g. lifecycle.json, then apply with AWS CLI:
aws s3api put-bucket-lifecycle-configuration \
--bucket my-versioned-bucket \
--lifecycle-configuration file://lifecycle.json
Confirm:
aws s3api get-bucket-lifecycle-configuration \
--bucket my-versioned-bucket
⏱ What happens next
Example: you upload photo.jpg several times.
| Version | Status | Age | Action |
|---|---|---|---|
| v5 | current | — | kept |
| v4 | noncurrent | < 1 day | kept (still too new) |
| v3 | noncurrent | > 1 day | deleted (more than 1 newer noncurrent version) |
| v2, v1 | noncurrent | old | deleted |
Result → only v5 and v4 remain.
⚠️ Notes / Caveats
- The rule only affects noncurrent versions, never the current one.
- Deletion isn’t instant; S3 processes lifecycle actions daily.
- You must have versioning enabled on the bucket.
- The combination of
NoncurrentDays+NewerNoncurrentVersionsdetermines exactly when deletions occur.
✅ Summary
| Goal | Supported by Lifecycle? | How |
|---|---|---|
| Keep exactly two most recent versions | ✅ (approximate, officially supported) | Use NoncurrentVersionExpiration with NewerNoncurrentVersions=1
|
Top comments (0)