Automation

Azure Container Registry: validate a purge before deleting rollback images

A production runbook for inventorying active digests, qualifying retention policy and acr purge, reviewing filters in dry-run, then enabling or rolling back ACR cleanup without breaking redeployments.

17 Oct 2026 azureacrcontainer-registrycontainersaksdevopsautomationretentionsupply-chainobservabilityrunbookrollbackproduction

An ACR registry keeps growing, storage cost rises, and thousands of build tags no longer appear useful. An acr purge task looks like low-risk housekeeping. A broad filter can nevertheless remove a manifest still referenced by a deployment, a dormant environment or the last qualified rollback. The pipeline remains green until the next restart, when the cluster can no longer pull the expected image.

The running case is a platform that publishes several images every day, deploys to AKS by digest and retains two rollback releases. The goal is not to keep every artifact. It is to prove what can be removed, exercise the selection without writes, canary the cleanup, then decide whether to enable, correct or abandon the purge.

Freeze the cleanup contract

Start with one explicit window and scope. Do not combine tag deletion, untagged manifest deletion, automated retention and deleted-artifact recovery into one change request.

yaml 01-acr-purge-contract.yml
registry: acrplatformprod
change_window_utc: 2026-10-17T20:00:00Z/2026-10-17T21:00:00Z
repositories:
- platform/orders-api
- platform/orders-worker
selection:
tag_pattern: "^(pr-|build-)"
older_than: 30d
delete_untagged_manifests: false
protected:
- every_running_digest
- current_release_digest
- two_qualified_rollback_digests
- active_canary_digest
- incident_hold_digests
forbidden:
- latest
- release-*
- include_locked
decision: pending

Retain the exact command, the acr-cli image version, the execution identity and the dry-run output. “Delete images older than 30 days” is not a contract: tag age, manifest age and release age describe different objects.

Inventory consumers before the registry

ACR cannot determine on its own whether a digest is still required. Build the protected set first from the systems that consume or promote images: running workloads, GitOps manifests, active releases, recovery environments, canaries and the rollback catalog.

bash 02-running-image-digests.sh
set -eu

kubectl get pods --all-namespaces -o json | jq -r '.items[]
    | .metadata.namespace as $ns
    | .metadata.name as $pod
    | .status.containerStatuses[]?
    | select(.imageID != null)
    | [$ns, $pod, .name, .image, .imageID]
    | @tsv' | sort -u > running-image-digests.tsv

# Add digests from release records, GitOps manifests, DR environments
# and rollback decisions to the same inventory.
sha256sum running-image-digests.tsv

The imageID observed on pods provides runtime evidence, but it does not cover a stopped environment or a release ready for redeployment. The final inventory must therefore bind every digest to an owner, a retention reason and a protection expiry.

Separate tags, manifests and recoverable data

A tag is a mutable pointer. Removing a tag does not necessarily delete its underlying manifest immediately. Deleting a manifest, however, makes a digest pull impossible even when a deployment still retains that reference.

Keep three mechanisms separate:

  • retention policy targets untagged manifests under the conditions supported by the registry;
  • acr purge selects tags with filters and age, and can also remove untagged manifests;
  • soft delete policy temporarily retains some deleted artifacts for recovery.

Do not assume retention policy and soft delete policy can be enabled together. Record their effective state before the purge, together with SKU, manifest types and lock attributes. Never describe a permanent deletion as rollbackable without a tested restoration path.

bash 03-acr-policy-and-manifest-inventory.sh
set -eu

ACR="acrplatformprod"

az acr show --name "$ACR" --query '{sku:sku.name,roleAssignmentMode:roleAssignmentMode}' -o json

az acr config retention show --registry "$ACR" -o json
az acr config soft-delete show --registry "$ACR" -o json

az acr repository list --name "$ACR" -o tsv | while IFS= read -r repo; do
    az acr manifest list-metadata       --registry "$ACR"       --name "$repo"       --query '[].{digest:digest,tags:tags,lastUpdate:lastUpdateTime,deleteEnabled:changeableAttributes.deleteEnabled}'       -o json       | jq --arg repository "$repo" 'map(. + {repository: $repository}) | .[]'
  done > acr-manifest-inventory.jsonl

Adapt pagination and output handling to the actual volume. A truncated inventory is a reason to block the purge, not an acceptable estimate.

Produce a reviewable dry run

Use explicit repositories and tag families. Avoid a registry-wide filter until the team can prove that every namespace follows the same retention contract.

acr purge is a preview feature. Treat its version, parameters and observed behavior as change inputs, not as an implicit stable interface.

bash 04-acr-purge-dry-run.sh
set -eu

ACR="acrplatformprod"

PURGE_CMD="acr purge --filter 'platform/orders-api:^(pr-|build-).*' --filter 'platform/orders-worker:^(pr-|build-).*' --ago 30d --keep 20 --dry-run"

az acr run --registry "$ACR" --cmd "$PURGE_CMD" --timeout 3600 /dev/null | tee acr-purge-dry-run.log

Do not add --untagged by reflex. Prove the targeted tag deletion first. Then analyze manifests that become untagged and whether any consumer protects them by digest. Keep --include-locked absent: that option turns an explicit protection into a barrier the task silently removes.

Extract the candidate list from the dry run and compare it with the protected set. Validation must fail on any intersection, unknown digest or output that cannot be parsed completely.

text 05-purge-review-gates.txt
Block the purge when
A running or rollback digest appears in candidates
A selected repository has no owner
The dry run is truncated, expired or used another command
A filter covers release, latest, canary or incident-hold
Canary restoration has not been tested
The identity can delete outside the approved scope

Allow the canary when
The command and observed acr-cli version are recorded
The protected set is timestamped and signed by the pipeline
Candidates have the expected age and tag family
Negative controls are never selected

Canary a repository with no production dependency

Create a test repository with the same tag, manifest and lock shapes. Publish at least one disposable artifact, one item preserved by --keep, one untagged digest and one locked artifact. Run the dry run, then the real command only against that repository.

After the canary, prove four outcomes: the candidate disappeared, the preserved control can still be pulled by digest, the lock was not changed, and task logs identify the exact identity, command and deletions.

Exercise recovery before production as well. When soft delete is enabled and applicable, restore the canary manifest and pull it by digest. Otherwise, reimport the same digest from the documented trusted source. Recreating a tag on a rebuilt image is not restoration of the same artifact.

Roll out in observable batches

Remove --dry-run only after the candidate file is approved. Keep the first execution to one repository, bound concurrency, and stop between batches to inspect pulls, deployments and registry errors.

yaml 06-acr-purge-rollout.yml
batch_1:
repository: platform/orders-worker
command_digest: <sha256-of-reviewed-command>
candidate_count: <count>
protected_intersection: 0
observe_for: 30m
validate:
  - current_digest_pull_succeeds
  - rollback_digest_pull_succeeds
  - no_image_pull_failures
  - task_deleted_expected_count_only
stop_on:
- unexpected_repository
- unexpected_manifest
- pull_failure
- partial_or_timed_out_task
next_batch_requires: explicit_approval

A timeout does not mean nothing was deleted. Treat it as a partial execution: freeze the logs and result, rebuild the inventory, then calculate the next batch again. Never rerun the same command blindly.

Decide, recover or roll back

text 07-acr-purge-decision.txt
ENABLE
The canary passes
No candidate intersects the protected set
Current and rollback pulls succeed after every batch
The scheduled task uses the approved command by digest

CORRECT
The filter selects a release or canary tag
--keep does not match the rollback requirement
OCI or untagged manifests require a separate policy
The execution identity has an overbroad deletion scope

STOP AND RECOVER
A protected digest disappeared
The next rollout or restart cannot pull its image
The deletion count differs from the candidate file
The task is partial or its logs are incomplete

ABANDON
No reliable consumer inventory exists
Recovery cannot be tested
Storage savings do not justify delivery risk

Operational rollback starts by disabling the scheduled task and freezing further deletion. Restore the digest from soft delete when it is genuinely available, or import it from an approved immutable source. Verify the digest, recreate only the required tags, then test a pull and a canary redeployment.

Conclusion

An ACR purge is a supply-chain operation, not simple storage housekeeping. Useful evidence links real consumers, release and rollback digests, the registry inventory, the dry-run selection and the result of every batch.

The final decision becomes explicit: enable cleanup when no protected artifact is selected and recovery works, correct an overbroad filter or scope, or stop the purge as soon as the registry can no longer guarantee redeployment by the same digest.