# 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](lifecycle-policies.md). 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:PutLifecyclePolicy` on the repository; to remove its own policy,
  `ecr:DeleteLifecyclePolicy`.
- To see the policy and its dry run: `ecr:GetLifecyclePolicy` on the repository. Without it the
  console's **Lifecycle policy** tab says so.
- In the console, also `ecr:DescribeRepositories` on the repository; without it its page answers 404.

## Set the policy

**Console**
1. Open **Image repositories**, then open the repository.
2. Open the **Lifecycle policy** tab.
3. Turn on **Customize for this repository**. The rules start as a copy of the organization
   default.
4. 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.
5. Check **Dry run: what this policy would do now**: every image with whether it would be deleted
   and what decides it.
6. 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.

**CLI**
```sh
dcloud ecr lifecycle-policy preview repository-id --policy @policy.json
dcloud ecr lifecycle-policy put repository-id --policy @policy.json
```

`policy.json` holds the policy itself, `{ "rules": [ … ] }`.

**API**
```http
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.

**Terraform**
```hcl
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

**Console**
1. On the **Lifecycle policy** tab, turn off **Customize for this repository**.

**CLI**
```sh
dcloud ecr lifecycle-policy set-mode repository-id --mode none
dcloud ecr lifecycle-policy delete repository-id
```

`set-mode` with `inherit` or `none` keeps a stored own policy, unused; `delete` removes it and
returns the repository to `inherit`.

**API**
```http
PUT /ecr/api/repositories/repository-id/lifecycle-mode
{ "mode": "none" }
```

**Terraform**
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

- `400` with the problems when the policy is neither a valid `ews-lifecycle/v1` policy nor a valid
  AWS ECR policy (in the console, **Save policy** stays disabled and the form shows the
  problems).
- `403` without `ecr:PutLifecyclePolicy` (`ecr:DeleteLifecyclePolicy` for `delete`).

## Related

- [Restore an expired image](restore-an-expired-image.md)
- [Set the organization default](set-the-organization-default.md)
- `ecr.lifecycle-policy.put` · `dcloud ecr lifecycle-policy put` · `dcloud_ecr_lifecycle_policy`
