> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/artifact-registry/use-artifact-registry/lifecycle-rules/create-cleanup-rule.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# Create a cleanup rule

Create a lifecycle cleanup rule to delete stale versions. Actively deployed Docker versions stay protected automatically.

A cleanup rule deletes artifact versions that match your cleanup criteria on a recurring schedule. Artifacts in registries that support soft deletion remain recoverable during the configured recovery period.

For Docker registries, the rule is deployment aware. When lifecycle rules are enabled, each run queries Harness Continuous Delivery (CD) and marks Docker versions that are actively deployed as **PROTECTED**. You do not configure a retention rule for this protection.

***

### Before you begin <a href="#before-you-begin" id="before-you-begin"></a>

Ensure you have the following before creating a cleanup rule:

* **Artifact Registry access:** Project Admin or Artifact Registry Administrator role. Go to [RBAC in Harness](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/platform-access-control/rbac-in-harness) to configure roles.
* **At least one registry:** You need an existing registry to attach the rule to. Go to [Create a Registry](https://developer.harness.io/artifact-registry/use-artifact-registry/manage-registries/create-registry.md) to set one up.

***

### Name the rule <a href="#name-the-rule" id="name-the-rule"></a>

1. Go to **Artifact Registry** > **Registries Settings** > **Lifecycle Rules**.
2. Select **+ New Lifecycle Rule**.
3. Enter a **Name** for the rule.
4. Add a **Description** (optional).
5. Under **Target**, select **Cleanup Rule**.
6. Select **Next**.

<figure><img src="https://4191040053-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGmEmgYs6IUcqNiV0OEgB%2Fuploads%2Fgit-blob-edfadf51fecdf5df424730c748553fd9b79cf677%2Fcleanup-step1-about-rule.png?alt=media" alt="About Rule step showing name, description, and target selection with Cleanup Rule selected"><figcaption><p>Step 1: Name the rule and select Cleanup Rule as the target</p></figcaption></figure>

***

### Configure scope and criteria <a href="#configure-scope-and-criteria" id="configure-scope-and-criteria"></a>

Configure what gets retained and what gets cleaned.

<figure><img src="https://4191040053-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGmEmgYs6IUcqNiV0OEgB%2Fuploads%2Fgit-blob-b2f6dae4dac151e62de1b600f554c2926ad1b275%2Fcleanup-step2-scope-criteria.png?alt=media" alt="Scope And Criteria step showing registry scope, package type, pattern filters, and cleanup criteria"><figcaption><p>Step 2: Define scope, filters, and cleanup criteria</p></figcaption></figure>

#### Registry scope <a href="#registry-scope" id="registry-scope"></a>

Select which registries this rule applies to:

* **All Registries:** Applies to all registries under the current scope.
* **Specific Registries:** Select individual registries to apply the rule to. When you choose this option, a **Registries** field appears where you search and select registries. Selected registries appear as tags in the field.

<figure><img src="https://4191040053-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGmEmgYs6IUcqNiV0OEgB%2Fuploads%2Fgit-blob-156945f6a6de66bb7b4a20cd1083af79599da447%2Fcleanup-step2-specific-registries.png?alt=media" alt="Scope And Criteria step with Specific Registries selected, showing the swift registry and regex pattern filters"><figcaption><p>Scope and Criteria with specific registry selection</p></figcaption></figure>

#### Package type <a href="#package-type" id="package-type"></a>

Select **All Package Types** or choose a specific format (Docker, Maven, npm, etc.) to narrow the rule to a single package type.

#### Pattern filters <a href="#pattern-filters" id="pattern-filters"></a>

Optionally, restrict which packages and versions the rule evaluates. The rule applies only to artifacts that match these patterns. **Package Name Patterns** and **Version Patterns** accept regular expressions. Press **Enter** to add each pattern.

{% hint style="info" %}
**USE `.*` TO MATCH ANY CHARACTERS**

Pattern filters are regular expressions. Use `.*` to match any characters. Escape a literal `.` as `\.`.
{% endhint %}

The following examples show the regular expression to enter for a common pattern:

| Pattern      | Regular expression |
| ------------ | ------------------ |
| `prod-*`     | `prod-.*`          |
| `*-prod-*`   | `.*-prod-.*`       |
| `*prod*`     | `.*prod.*`         |
| `frontend-*` | `frontend-.*`      |
| `*-service`  | `.*-service`       |
| `1.0.*`      | `1\.0\..*`         |
| `*-SNAPSHOT` | `.*-SNAPSHOT`      |

#### Cleanup criteria <a href="#cleanup-criteria" id="cleanup-criteria"></a>

You must select at least one criterion.

{% hint style="info" %}
**AND LOGIC**

When multiple criteria are enabled, **all** conditions must be true for a version to be deleted. For example, "Keep last 10" and "Older than 30 days" means a version is only deleted if it is beyond the 10 most recent **and** older than 30 days.
{% endhint %}

* **Keep last N versions:** Retain the N most recently uploaded versions of the package, in upload order, not by semantic version. Versions beyond this count become deletion candidates.
* **Delete artifacts older than X days:** Target versions created before the specified threshold.
* **Delete if not downloaded in last X days:** Target versions with no download activity within the specified window.

{% hint style="info" %}
**ACTIVELY DEPLOYED DOCKER VERSIONS STAY PROTECTED**

On a Docker registry, a version that Harness CD is actively deploying is marked **PROTECTED** and skipped. This is automatic when lifecycle rules are enabled and a retention rule is not required.
{% endhint %}

Select **Next**.

***

### Set the schedule <a href="#set-the-schedule" id="set-the-schedule"></a>

Define when the rule executes.

<figure><img src="https://4191040053-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGmEmgYs6IUcqNiV0OEgB%2Fuploads%2Fgit-blob-b768e077e3722ebd591810972ab6b833af662786%2Fcleanup-step3-schedule.png?alt=media" alt="Schedule step showing timezone, frequency tabs, time picker, and generated cron expression"><figcaption><p>Step 3: Configure the execution schedule</p></figcaption></figure>

1. Select a **Timezone** (default: UTC).
2. Choose a frequency tab:
   * **Daily:** Set the time (hour, minute, AM/PM).
   * **Monthly:** Select the day of month and time.
   * **Yearly:** Select the month, day, and time.
   * **Custom:** Enter a cron expression directly.
3. Review the generated **Cron Expression** displayed below the time picker.
4. Select **Submit**.

The cleanup rule is created and executes as per the cron schedule. You can verify the rule on the Lifecycle Rules tab, where it shows the criteria summary, last run, and next run time.

{% hint style="info" %}
Use Dry Run from the rule actions menu (**⋮**) before the first scheduled execution to verify which artifacts would be affected.
{% endhint %}

***

### Next steps <a href="#next-steps" id="next-steps"></a>

* For worked cleanup and retention patterns, including snapshot cleanup and production tag protection, go to [Rule examples](https://developer.harness.io/artifact-registry/use-artifact-registry/lifecycle-rules.md#rule-examples).
* To understand rule evaluation logic, dry runs, and execution history, go to [Lifecycle rules overview](https://developer.harness.io/artifact-registry/use-artifact-registry/lifecycle-rules.md).
* To protect specific artifacts from cleanup, go to [Create a retention rule](https://developer.harness.io/artifact-registry/use-artifact-registry/lifecycle-rules/create-retention-rule.md).
* To understand soft delete recovery, go to [Delete Artifacts](https://developer.harness.io/artifact-registry/use-artifact-registry/manage-artifacts/soft-delete.md).

{% @harness-feedback/feedback %}
