Set a lifecycle policy
Give a repository its own rules for deleting old images, go back to the organization default, or let nothing expire.
How rules decide is in Lifecycle policies. Replace repository-id with the repository's id.
Warning
From the next hourly run, the images the policy selects are deleted. With softDeleteDays 0 they
cannot be restored.
Prerequisites
- Permission for
ecr:PutLifecyclePolicyon the repository; to remove its own policy,ecr:DeleteLifecyclePolicy. - To see the policy and its dry run:
ecr:GetLifecyclePolicyon the repository. Without it the console's Lifecycle policy tab says so. - In the console, also
ecr:DescribeRepositorieson the repository; without it its page answers 404.
Set the policy
- Open Image repositories, then open the repository.
- Open the Lifecycle policy tab.
- Turn on Customize for this repository. The rules start as a copy of the organization default.
- Edit the rules: under Always keep, what is never deleted; under Then delete, in this order, the rules (Add rule); under Undo window, how many days deleted images stay restorable. Or choose Advanced (JSON) and paste a policy, an AWS ECR one included.
- Check Dry run: what this policy would do now: every image with whether it would be deleted and what decides it.
- Choose Save policy.
Turning Customize for this repository off again makes the repository use the organization
default at once, without Save policy. Without ecr:PutLifecyclePolicy the switch and the rules
are read-only.
dcloud ecr lifecycle-policy preview repository-id --policy @policy.json
dcloud ecr lifecycle-policy put repository-id --policy @policy.jsonpolicy.json holds the policy itself, { "rules": [ … ] }.
PUT /ecr/api/repositories/repository-id/lifecycle-policy
{ "policy": { "rules": [ { "rulePriority": 1, "description": "Untagged images after 14 days", "selection": { "tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": 14 }, "action": { "type": "expire" } }, { "rulePriority": 2, "description": "Keep the last 30 release builds", "selection": { "tagStatus": "tagged", "tagPatternList": ["v*"], "countType": "imageCountMoreThan", "countNumber": 30 }, "action": { "type": "expire" } } ] } }POST …/lifecycle-policy/preview with the same body shows what it would do without saving.
resource "dcloud_ecr_lifecycle_policy" "my_app" {
repository_id = dcloud_ecr_repository.my_app.id
policy = jsonencode({
rules = [{
rulePriority = 1
selection = { tagStatus = "untagged", countType = "sinceImagePushed", countUnit = "days", countNumber = 14 }
action = { type = "expire" }
}]
})
}In the example, rule 2 also keeps the 30 newest v* images away from any later rule. An AWS ECR
policy is stored as you sent it, and its deleted images stay restorable for 7 days.
Go back to the default, or let nothing expire
- On the Lifecycle policy tab, turn off Customize for this repository.
dcloud ecr lifecycle-policy set-mode repository-id --mode none
dcloud ecr lifecycle-policy delete repository-idset-mode with inherit or none keeps a stored own policy, unused; delete removes it and
returns the repository to inherit.
PUT /ecr/api/repositories/repository-id/lifecycle-mode
{ "mode": "none" }Remove the dcloud_ecr_lifecycle_policy resource and apply.
none can be set through the CLI and the API only.
Result
The save answers with what the policy would delete right now (wouldExpire) and a decision for
every image. The next hourly run applies it, at most 1,000 images per repository a run. Each run
that deletes anything writes an event to the repository's activity, with the first images and why.
If it is refused
400with the problems when the policy is neither a validews-lifecycle/v1policy nor a valid AWS ECR policy (in the console, Save policy stays disabled and the form shows the problems).403withoutecr:PutLifecyclePolicy(ecr:DeleteLifecyclePolicyfordelete).