Create a cleanup rule
Create a lifecycle cleanup rule to automatically delete stale artifact versions on a schedule. Actively deployed Docker images stay protected.
A cleanup rule identifies artifact versions that match your deletion criteria and removes them on a recurring schedule. This keeps registries lean and storage costs predictable without requiring manual intervention.
Artifacts in registries that support soft deletion remain recoverable during the configured recovery period. For Docker registries, the rule is deployment-aware: each execution queries Harness CD and marks actively deployed versions as PROTECTED, skipping them automatically.
Before you begin
Artifact Registry access: Project Admin or Artifact Registry Administrator role. To configure roles, go to RBAC and permissions.
At least one registry: An existing registry to attach the rule to. To create one, go to Create a registry.
Name the rule
Go to Artifact Registry > Registries Settings > Lifecycle Rules.
Select + New Lifecycle Rule.
Enter a Name for the rule.
Add a Description (optional).
Under Target, select Cleanup Rule.
Select Next.
Configure scope and criteria
This step defines which registries and artifacts the rule targets, and what conditions trigger deletion.
Registry scope
Select which registries the rule applies to:
All Registries: Applies to every registry under the current scope. This selection is dynamic and automatically includes registries created after the rule.
Specific Registries: Select individual registries from the dropdown. Selected registries appear as tags in the field.
Package type
Select All Package Types or choose a specific format (Docker, Maven, npm, and others) to narrow the rule to a single package type.
Pattern filters
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. Type a pattern and press Enter to add it.
The following examples show common patterns and their regular expression equivalents:
prod-*
prod-.*
*-prod-*
.*-prod-.*
*prod*
.*prod.*
frontend-*
frontend-.*
*-service
.*-service
1.0.*
1\.0\..*
*-SNAPSHOT
.*-SNAPSHOT
Cleanup criteria
You must enable at least one criterion. When you enable more than one, all conditions must be true for a version to be deleted (AND logic).
Keep last N versions: Retains the N most recently uploaded versions of each package, ordered by upload timestamp, not by semantic version. Versions beyond this count become deletion candidates.
Delete artifacts older than X days: Targets versions whose upload timestamp is beyond the specified number of days.
Delete if not downloaded in last X days: Targets versions with no download activity within the specified window.
Select Next.
Set the schedule
Define when the rule executes.
Select a Timezone from the dropdown (default: UTC).
Choose a frequency tab:
Daily: Set the hour, minute, and AM/PM.
Monthly: Select the day of the month and time.
Yearly: Select the month, day, and time.
Custom: Enter a cron expression directly.
Review the generated Cron Expression displayed below the time picker to confirm the schedule.
Select Submit to create the cleanup rule.
The rule appears on the Lifecycle Rules tab with its criteria summary, last run time, and next scheduled run.
Rule examples
The following examples illustrate common cleanup setups. To understand the regular expressions used here, refer to the pattern filter table above.
Keep the last 10 uploaded versions, and delete versions older than 90 days
Enable both criteria:
Keep last N versions: 10
Delete artifacts older than X days: 90
Both must be true before deletion. Harness keeps the 10 most recently uploaded versions of each package, in upload order. A version outside those 10 is deleted only when it is also older than 90 days.
Delete unused Maven SNAPSHOT versions
Configure a cleanup rule for Maven with:
Version Patterns:
.*-SNAPSHOTDelete if not downloaded in last X days: 30
The rule evaluates only SNAPSHOT versions. Release versions are never candidates. A matching version is deleted when it has not been downloaded in 30 days.
Delete staging tags from a Docker registry
Create a cleanup rule with:
Tag Patterns:
staging-.*Delete artifacts older than X days: 14
Only Docker tags matching staging-* are evaluated. Production and latest tags are not candidates. To protect production tags explicitly, go to Create a retention rule.
Next steps
To protect specific artifacts from cleanup, go to Create a retention rule.
To validate which artifacts a rule targets, go to Run a dry run.
To review past cleanup executions, go to View rule executions.
For lifecycle concepts and additional examples, go to Lifecycle rules.
Last updated
Was this helpful?