# Updating a lifecycle policy

Once you create a [lifecycle policy](../../concepts/lifecycle-policy.md), you can edit its rules as well as its status, name, or description.

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) the [registry](../../concepts/registry.md) was created in.
  1. Navigate to **Container Registry**.
  1. Select the registry and click the row with its name.
  1. Select the [repository](../../concepts/repository.md) and click the row with its name.
  1. In the left-hand panel, click ![lifecycle](../../../_assets/console-icons/arrows-rotate-right.svg) **Lifecycle**.
  1. In the row with the lifecycle policy, click ![image](../../../_assets/console-icons/ellipsis.svg) and select **Edit**.
  1. Edit the lifecycle policy parameters:
     * **Name**.
     * **Description**.
     * **Status**.
     * Under **Lifecycle policy rules**, update the rule parameters:

       * **Tag regexp**: Docker image tag for filtering. Java regular expressions are supported. For example, the `test.*` regular expression retrieves all images with tags starting with `test`.
       * **Untagged**: Flag indicating that the rule applies to Docker images without tags.
       * **Expire period, in days**: Time after which the lifecycle policy may apply to the Docker image.
       * **Retained top**: Number of Docker images that are not deleted even if they match the rule.

       * **Description**.
  1. Click **Save**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  1. See the description of the [CLI](../../../cli/index.md) command for updating policy parameters:

     ```bash
     yc container repository lifecycle-policy update --help
     ```

  1. Change the policy parameters, e.g., rename it:

     ```bash
     yc container repository lifecycle-policy update <policy_ID> \
        --new-name=new-policy
     ```

     To find out the policy ID, get a [list of lifecycle policies in a repository or registry](lifecycle-policy-list.md#lifecycle-policy-list).

- Terraform {#tf}

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../../terraform/authentication.md) using the appropriate method.

  1. Open the configuration file and edit the fragment with the policy description:

     ```hcl
     resource "yandex_container_repository_lifecycle_policy" "my_lifecycle_policy" {
       name          = "best-policy"
       status        = "active"
       repository_id = "crpfvi6o4ra7********"

       rule {
         description   = "rule for applying policy"
         untagged      = true
         tag_regexp    = ".*"
         retained_top  = 1
         expire_period = "48h"
       }
     }
     ```

  1. Apply the changes:

     1. In the terminal, navigate to the configuration file directory.
     1. Make sure the configuration is correct using this command:
     
        ```bash
        terraform validate
        ```
     
        If the configuration is valid, you will get this message:
     
        ```bash
        Success! The configuration is valid.
        ```
     
     1. Run this command:
     
        ```bash
        terraform plan
        ```
     
        You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
     1. Apply the configuration changes:
     
        ```bash
        terraform apply
        ```
     
     1. Type `yes` and press **Enter** to confirm the changes.

  This will update the lifecycle policy in the specified [repository](../../concepts/repository.md). You can check the policy update using the [management console](https://console.yandex.cloud) or this [CLI](../../../cli/index.md) command:

  ```bash
  yc container repository lifecycle-policy list --registry-id <registry_ID>
  ```

- API {#api}

  To edit a lifecycle policy, use the [Update](../../api-ref/grpc/LifecyclePolicy/update.md) method for the [LifecyclePolicyService](../../api-ref/grpc/LifecyclePolicy/index.md) resource. Specify the policy ID in the `lifecycle_policy_id` property.

  You can retrieve a list of policies using the [List](../../api-ref/grpc/LifecyclePolicy/list.md) method for the [LifecyclePolicyService](../../api-ref/grpc/LifecyclePolicy/index.md) resource.

{% endlist %}

## Examples {#examples}

### Updating lifecycle policy rules {#update-rules}

{% list tabs group=instructions %}

- CLI {#cli}

  1. Prepare new [policy rules](../../concepts/lifecycle-policy.md#lifecycle-rules) and save them to a file named `new-rules.json`.

     Example of the contents of a file with rules, where:
     * `description`: Description of the policy rule.
     * `tag_regexp`: Docker image tag for filtering. Java regular expressions are supported. For example, the `test.*` regular expression retrieves all images with tags starting with `test`.
     * `untagged`: Flag indicating that the rule applies to Docker images without tags.
     * `expire_period`: Time after which the lifecycle policy may apply to the Docker image. This parameter is formatted as a number and a unit of measure, such as `s`, `m`, `h`, or `d` (seconds, minutes, hours, or days). `expire_period` must be a multiple of 24 hours.
     * `retained_top`: Number of Docker images that are not deleted even if they match the rule.
     
     ```json
     [
       {
         "description": "delete prod Docker images older than 60 days but retain 20 last ones",
         "tag_regexp": "prod",
         "expire_period": "60d",
         "retained_top": 20
       },
       {
         "description": "delete all test Docker images except 10 last ones",
         "tag_regexp": "test.*",
         "retained_top": 10
       },
       {
         "description": "delete all untagged Docker images older than 48 hours",
         "untagged": true,
         "expire_period": "48h"
       }
     ]
     ```

  1. Change the policy rules by running the command with the following parameter:
     * `new-rules`: Path to the file with the new policy description.

     {% note alert %}

     Updating the policy completely overwrites all current rules by destroying them.

     {% endnote %}

     ```bash
     yc container repository lifecycle-policy update crp6lg1868p3******** --new-rules ./new-rules.json
     ```

     Result:

     ```text
     WARN: All current lifecycle rules will be overwritten. Are you sure?[y/N] y
     id: crp6lg1868p3********
     name: test-policy
     ...
       expire_period: 172800s
       tag_regexp: test.*
       untagged: true
     ```

{% endlist %}

### Updating a lifecycle policy status {#update-status}

#### Activate a lifecycle policy {#activate}

{% list tabs group=instructions %}

- CLI {#cli}

  Activate a deactivated lifecycle policy by running the command below with the `--activate` flag:

  ```bash
  yc container repository lifecycle-policy update crp6lg1868p3******** --activate
  ```

  Result:

  ```text
  id: crp6lg1868p3********
  name: test-policy
  repository_id: crp3cpm16edq********
  ...
    expire_period: 172800s
    tag_regexp: test.*
    untagged: true
  ```

{% endlist %}

#### Disable the lifecycle policy {#disabled}

{% list tabs group=instructions %}

- CLI {#cli}

  Deactivate an active policy by running the command below with the `--disable` flag:

  ```bash
  yc container repository lifecycle-policy update crp6lg1868p3******** --disable
  ```

  Result:

  ```text
  id: crp6lg1868p3********
  name: test-policy
  repository_id: crp3cpm16edq********
  ...
    expire_period: 172800s
    tag_regexp: test.*
    untagged: true
  ```

{% endlist %}

### Changing a lifecycle policy name {#update-name}

{% list tabs group=instructions %}

- CLI {#cli}

  Change the policy name by running the command:

  ```bash
  yc container repository lifecycle-policy update crp6lg1868p3******** --new-name new-policy
  ```

  Where `--new-name` is the new policy name. The naming requirements are as follows:

  * Length: between 3 and 63 characters.
  * It can only contain lowercase Latin letters, numbers, and hyphens.
  * It must start with a letter and cannot end with a hyphen.

  Result:

  ```text
  id: crp6lg1868p3********
  name: new-policy
  repository_id: crp3cpm16edq********
  ...
    expire_period: 172800s
    tag_regexp: test.*
    untagged: true
  ```

{% endlist %}

### Updating a lifecycle policy description {#update-description}

{% list tabs group=instructions %}

- CLI {#cli}

  Change a policy description by running the command:

  ```bash
  yc container repository lifecycle-policy update crp6lg1868p3******** --new-description "new description"
  ```

  Where `--new-description` is the new policy description.

  Result:

  ```text
  id: crp6lg1868p3********
  name: test-policy
  repository_id: crp3cpm16edq********
  ...
    expire_period: 172800s
    tag_regexp: test.*
    untagged: true
  ```

{% endlist %}