Retention and Cleanup Policies for Registry Repositories
Designing container registry lifecycle rules, automated retention policies, purging stale PR tags, and balancing storage costs across AWS ECR, Harbor, and Docker Hub.
Published on • November 18, 2026
AI Assistant

In active continuous integration environments, automated pipelines push dozens or hundreds of container image builds every day. Without automated Registry Retention Policies, storage consumption quickly reaches terabytes, leading to exorbitant cloud storage bills and registry performance degradation.
In this guide, we will design an enterprise image retention matrix, configure automated lifecycle rules across AWS ECR, Harbor, and Docker Hub, and prevent accidental deletion of critical production images.
Enterprise Image Retention Matrix
Not all container image tags carry the same long-term value. A production release tag must be preserved permanently, while a temporary pull request build tag can be safely purged after a few days.
+-----------------------------------------------------------------------------------+
| Recommended Tiered Retention Matrix |
+-----------------------------------------------------------------------------------+
| Tag Category | Match Pattern | Retention Rule | Rationale |
+-------------------+------------------+--------------------------+-----------------+
| Production Semver | `v*.*.*` | Retain Permanently | Release history |
| Floating Major | `v1`, `v2` | Retain Last 5 Pushes | Active release |
| Main/Edge Builds | `edge`, `main-*` | Retain Last 10 Pushes | Staging deployments|
| PR / Feature Tags | `pr-*`, `feat-*` | Delete after 7-14 Days | Transient builds|
| Untagged Blobs | `<untagged>` | Delete after 1-3 Days | Orphaned layers |
+-----------------------------------------------------------------------------------+
Configuring AWS ECR Lifecycle Policies
AWS Elastic Container Registry (ECR) manages image retention via JSON-formatted Lifecycle Policies.
Example ECR Lifecycle Policy (ecr-policy.json)
{
"rules": [
{
"rulePriority": 1,
"description": "Purge untagged intermediate images after 1 day",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 1
},
"action": {
"type": "expire"
}
},
{
"rulePriority": 2,
"description": "Purge pull request build tags after 7 days",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["pr-", "feat-"],
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 7
},
"action": {
"type": "expire"
}
},
{
"rulePriority": 3,
"description": "Retain only the last 10 main branch images",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["main-"],
"countType": "imageCountMoreThan",
"countNumber": 10
},
"action": {
"type": "expire"
}
}
]
}
Apply policy to ECR repository via AWS CLI:
aws ecr put-lifecycle-policy \
--repository-name payment-service \
--lifecycle-policy-text file://ecr-policy.json
Configuring Retention Policies in Project Harbor
Harbor includes a visual Tag Retention Engine with boolean rule matching.
Recommended Harbor Project Policy Setup:
Rule 1: Always retain artifacts tagged with pattern `v*.*.*`
Rule 2: Retain top 10 most recently pushed artifacts with pattern `main-*`
Rule 3: Retain artifacts pulled within the last 30 days
Rule 4: Purge all artifacts matching `pr-*` older than 7 days
Schedule Garbage Collection (GC) to run automatically 1 hour after the retention policy execution window.
Protecting Critical Images Against Deletion
Automated cleanup rules can be dangerous if misconfigured. Use these safeguards to protect production assets:
- Enable Image Tag Immutability: Configure production release patterns (
v*.*.*) as Immutable in ECR, Harbor, or ACR so they cannot be overwritten or accidentally deleted. - Dry-Run Testing: Test new retention rules in dry-run mode before enabling hard deletion.
- Keep Untagged Retention Buffer: Always set a 24-48 hour buffer before purging untagged images to prevent deleting layers currently being pushed by active CI builds.
Cost Impact Analysis
Before Retention Policy (1 Year):
- 10 builds/day * 500MB = 5 GB/day -> 1.8 TB/year -> ~$90/month in cloud storage fees.
After Implementing Retention Policy:
- Preserves 20 semver tags + 10 main tags + 7 days PRs = ~30 GB steady state -> ~$1.50/month!
- 98% Reduction in Storage Costs!
Summary
Implementing a tiered retention policy and automated lifecycle cleanup stops registry storage bloat, reduces cloud infrastructure spend, and keeps container registries clean and responsive.