Sub topic 10
Cleaning up with lifecycle policies
Every docker push adds data to a repository, and ECR bills you for the storage. CI pipelines that push on every commit can leave thousands of old images behind. A lifecycle policy tells ECR to delete images automatically based on rules you write.
This chapter assumes you can push images to a repository and know the difference between a tag and a digest.
How a lifecycle policy works
A lifecycle policy belongs to one repository. It is a JSON document with a list of rules. Each rule has three parts:
rulePriority: a unique number. ECR evaluates rules from the lowest number to the highest.selection: which images the rule applies to, and when they count as old.action: alwaysexpire, which deletes the image.
The selection block uses these fields:
| Field | Meaning |
|---|---|
| `tagStatus` | `tagged`, `untagged`, or `any` |
| `tagPrefixList` or `tagPatternList` | Tag filters. One of them is required when `tagStatus` is `tagged`. |
| `countType` | `sinceImagePushed` (age in days) or `imageCountMoreThan` (keep only the newest N) |
| `countUnit` | `days`. Used only with `sinceImagePushed`. |
| `countNumber` | The age in days, or the number of images to keep |
A two-rule policy
This policy keeps the 10 newest images whose tags start with v, and deletes untagged images after 14 days.
{
"rules": [
{
"rulePriority": 1,
"description": "Keep only the 10 newest release images",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["v"],
"countType": "imageCountMoreThan",
"countNumber": 10
},
"action": {
"type": "expire"
}
},
{
"rulePriority": 2,
"description": "Expire untagged images 14 days after they are pushed",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}Untagged images are usually the old copies left behind when you push a new image under an existing mutable tag. The tag moves to the new image, and the old image becomes untagged.
Preview before you apply
A bad rule can delete images you still need, and deletion cannot be undone. Run a preview first. It reports what the policy would expire and changes nothing.
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-1The preview runs asynchronously. If the second command shows a status of IN_PROGRESS, run it again. When the status is COMPLETE, check the list of images it would expire. Then apply the policy:
aws ecr put-lifecycle-policy \
--repository-name my-app \
--lifecycle-policy-text file://policy.json \
--region us-east-1Common errors and gotchas
- Rule order matters. A rule with
tagStatusset toanymatches every image, so it must have the highest priority number. Otherwise it would shadow the rules after it. ECR rejects a policy that breaks this. - Missing tag filter. A
taggedrule withouttagPrefixListortagPatternListis invalid. - Deletion is not immediate. ECR evaluates policies periodically, so expired images can remain for a while after they qualify.
- Policies ignore usage. ECR does not check whether a running ECS task or EKS pod uses an image. If you expire an image that is still deployed, the next pull of it fails, for example when a node is replaced.
Exercise
Your repository holds tags like v1, v2, and so on, plus many untagged images. You want to keep only the 5 newest v images and delete everything else that is untagged after 7 days. Which countType does each rule use, and which rule gets the lower priority number if you also add a catch-all rule with tagStatus set to any?
Show answer
The v rule uses imageCountMoreThan with countNumber 5. The untagged rule uses sinceImagePushed with countUnit days and countNumber 7. The catch-all any rule must have the highest priority number, so both specific rules get lower numbers.