Sub topic 12
Cleaning up old images with lifecycle policies
This chapter assumes you can push images to a registry and that you have an ECR repository from the earlier chapter. You should also know what a tag and a digest are. Every push adds layers and manifests, and nothing removes them for you. Over months, CI builds pile up and storage costs grow. A lifecycle policy tells the registry to delete images automatically based on rules you define.
How a lifecycle policy works
An Amazon ECR lifecycle policy is a JSON document attached to one repository. It holds a list of rules. Each rule has these parts:
- A priority number. ECR evaluates rules from the lowest number to the highest.
- A selection, which says which images the rule matches.
- An action. ECR supports only
expire, which deletes the matched images.
The selection has a tagStatus of tagged, untagged, or any. It also has a countType, which is either sinceImagePushed (age in days) or imageCountMoreThan (keep only the newest N).
ECR applies policies on its own schedule. Expiration is not instant, so a matching image can remain for a short time after it qualifies.
Example: expire untagged images
Untagged images appear when you push a new image under an existing tag. The old image loses the tag but stays in storage. This policy removes untagged images 14 days after they were pushed.
{
"rules": [
{
"rulePriority": 1,
"description": "Expire untagged images older than 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}Save it as policy.json and apply it:
aws ecr put-lifecycle-policy \
--repository-name my-app \
--lifecycle-policy-text file://policy.json \
--region us-east-1Preview before you apply
Expiration cannot be undone. ECR can show what a policy would delete without deleting anything:
aws ecr start-lifecycle-policy-preview \
--repository-name my-app \
--lifecycle-policy-text file://policy.json \
--region us-east-1
aws ecr get-lifecycle-policy-preview \
--repository-name my-app \
--region us-east-1Run the second command again if the preview status is still IN_PROGRESS. Check the listed images against what you expect before you run put-lifecycle-policy.
Common mistakes
- Expiring images that are still deployed. An age-based rule on tagged images can delete a release that is still running. A node that restarts then fails with an image pull error. Use count-based rules for release tags, and keep the newest several.
- Rule ordering. A broad rule with a low priority number can take images before a narrower rule sees them. Put specific rules first.
- Expecting tag immutability to protect images. Immutability stops a tag from being overwritten. It does not stop a lifecycle policy from expiring that tag.
- Assuming other registries behave the same way. This feature is specific to ECR. GHCR has no equivalent policy language, so you clean up there with package retention settings, the API, or a scheduled workflow.
Where this fits
Lifecycle policies work alongside tag immutability and scanning, which the next chapter covers. Together they keep a repository small, predictable, and checked for vulnerabilities.
Exercise
A repository holds release images tagged v1.0.0, v1.1.0, and so on, plus many untagged images left over from CI. You want to delete untagged images after 7 days and keep only the 10 newest tagged images. How would you structure the rules, and why?
Show answer
Use two rules. Rule 1 has tagStatus: untagged, countType: sinceImagePushed, countUnit: days, countNumber: 7. Rule 2 has tagStatus: tagged with tagPrefixList: ["v"], countType: imageCountMoreThan, countNumber: 10. Give them different priorities. A count-based rule for release tags keeps the newest releases regardless of age, so a long-running deployment is not deleted just because it is old. Run start-lifecycle-policy-preview first to confirm the matches.