[Yandex Cloud documentation](../../../index.md) > [Yandex Container Registry](../../index.md) > [Step-by-step guides](../index.md) > Managing Docker image lifecycle policies > Creating a lifecycle policy

# Creating a lifecycle policy

You can only set a [lifecycle policy](../../concepts/lifecycle-policy.md) for a [repository](../../concepts/repository.md). To find out the name of a repository, request a [list of repositories in the registry](../repository/repository-list.md#repository-get).

{% 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 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 top-right corner, click **Create**.
  1. Set the lifecycle policy parameters:
     * **Name**. This is an optional setting.
     * **Description**. This is an optional setting.
     * **Status**: Lifecycle policy status after its creation. We do not recommend creating an `ACTIVE` policy right away.
     * Under **Lifecycle policy rules**, add rules:
       1. Click **Add**.
       1. Set 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**. This is an optional setting.
  1. Click **Create**.

- CLI {#cli}

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

  1. Prepare [policy rules](../../concepts/lifecycle-policy.md#lifecycle-rules) and save them to a file named `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. Create a lifecycle policy by running the command:

     ```bash
     yc container repository lifecycle-policy create \
       --repository-name crp3cpm16edq********/ubuntu \
       --name test-policy \
       --description "disabled lifecycle-policy for tests" \
       --rules ./rules.json
     ```

     Where:
     * `--repository-name`: Repository name.
     * `--rules`: Path to the policy description file.
     * `--description` (optional): Lifecycle policy description.
     * `--name` (optional): 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.

     {% note info %}

     The default policy is created disabled (`DISABLED` status). We do not recommend creating an active policy (`--active` flag) right away.

     {% endnote %}

     Result:

     ```text
     id: crp6lg1868p3********
     name: test-policy
     repository_id: crp3cpm16edq********
     ...
     - description: delete all untagged Docker images older than 48 hours
       expire_period: 172800s
       untagged: true
     ```

     The `expired_period` parameter value in the response is displayed in seconds. This is a technical constraint, the format will be changed.
  1. Make sure that the policy is created by running the command:

     ```bash
     yc container repository lifecycle-policy list --repository-name crp3cpm16edq********/ubuntu
     ```

     Where `--repository-name` is the repository name.

     Result:

     ```text
     +----------------------+-------------+----------------------+----------+---------------------+-------------------------------+
     |          ID          |    NAME     |    REPOSITORY ID     |  STATUS  |       CREATED       |          DESCRIPTION          |
     +----------------------+-------------+----------------------+----------+---------------------+-------------------------------+
     | crp6lg1868p3******** | test-policy | crp3cpm16edq******** | DISABLED | 2020-05-28 15:05:58 | disabled lifecycle-policy for |
     |                      |             |                      |          |                     | tests                         |
     +----------------------+-------------+----------------------+----------+---------------------+-------------------------------+
     ```

- 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. In the configuration file, specify the properties of the resources you want to create:

     ```hcl
     resource "yandex_container_repository_lifecycle_policy" "my_lifecycle_policy" {
       name          = "<policy_name>"
       status        = "<policy_status>"
       repository_id = "<repository_ID>"

       rule {
         description   = "<rule_description>"
         untagged      = true
         tag_regexp    = ".*"
         retained_top  = 1
         expire_period = "48h"
       }
     }
     ```

     Where:
     * `name`: Policy name.
     * `status`: Policy status. It can either be `true` or `false`.
     * `repository_id`: Repository ID.
     * `rule`: Section with the policy rule. Contains the following:
       * `description`: Rule description.
       * `untagged`: If the parameter is set to `true`, the rule applies to all [Docker images](../../concepts/docker-image.md) that do not have a [tag](../../concepts/docker-image.md#version).
       * `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`.
       * `retained_top`: Number of Docker images that will not be deleted even if they match the lifecycle policy rules.
       * `expire_period`: Time after which the lifecycle policy applies to the Docker image. This parameter comes as a number followed by a unit of measurement: `s`, `m`, `h`, or `d` (seconds, minutes, hours, or days). `expire_period` must be a multiple of 24 hours.

     For more information about `yandex_container_repository_lifecycle_policy` properties in Terraform, see [this provider guide](../../../terraform/resources/container_repository_lifecycle_policy.md).
  1. Create the resources:

     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 create a lifecycle policy in the specified repository. You can check the new policy and its settings 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 create a lifecycle policy, use the [Create](../../api-ref/grpc/LifecyclePolicy/create.md) method for the [LifecyclePolicyService](../../api-ref/grpc/LifecyclePolicy/index.md) resource.

{% endlist %}

{% note tip %}

You can [test the lifecycle policy](lifecycle-policy-dry-run.md) to check what [Docker images](../../concepts/docker-image.md) comply with the policy rules. Docker images are not actually deleted during dry runs.

{% endnote %}