In this blog post, we will see how to purge container images from Azure Container Registry (ACR). Till recent time most of them would have written custom PowerShell or bash script and run it to clean (purge) ACR container images. Recently Microsoft introduced a new preview feature to automatically purge images based on filters, duration, and untagged manifests. Additionally, this new preview feature supports to run on-demand or on a schedule using CRON expression.
In my current project, our delivery team is committing code several times a day. This automatically triggers a CI pipeline from which images are automatically built and pushed into ACR. Most of them would be going through the same journey. In this situation using this new preview feature, helps to keep ACR clean and not worrying about the Container registry getting bloated with images.
Both on-demand or schedule will make use of acr purge command. ACR purge is designed to run as a container command in an ACR Task internally it authenticates automatically with the registry where the task runs and performs actions there. Purge command options and capabilities are defined here.
Note: Use the ACR purge command with caution — deleted image data is UNRECOVERABLE.
On-Demand Task
On-demand task as the name says if you want to run on-demand or ad-hoc basis to purge container images based on the filter criteria defined.
Scheduled Task
Using the ACR purge command we can also schedule the purge to run automatically on a specific schedule. This is based on running ACR tasks. Schedule parameter in the below command is defined in Cron expressions format and you can read more about them at https://docs.microsoft.com/en-us/azure/container-registry/container-registry-tasks-scheduled#cron-expressions.
View the list of Scheduled Task
az acr task list -r <ACR Registry Name> -o table
Delete the Scheduled Task
az acr task delete --name <Task Name> --registry <ACR Registry Name>
References:
Retention policy for untagged manifests
Originally published at https://rajurh.blogspot.com on January 4, 2021.