Delete images from Azure Container Registry

Introduction

Azure container registry is used to manage container images and artifacts. It can connect with environments like Azure app service, Kubernetes Service, Red Hat OpenShift, and many others.

Whenever you push the images to the Azure container registry repositories, it will add the tag name as 'latest' to the images by default. When you keep pushing the images, it will remove the tags from the old image and apply the latest tag to the new image.

It is always a good practice to clean and manage the repository space.

In this blog, you will learn how to remove all images except the latest one.

How to Delete Images from Azure Container Registry?

As per the below image, we have two images in the ACR repository.

Docker Application

Use the below Azure CLI command to initiate the dry run, this will give you the result of a number of tags, and the manifest will be removed from the repository.

az acr run --registry <ACR Name> --cmd 'acr purge --filter <repository name>:.*  --ago 0d --untagged -keep 1 --dry-run' /dev/null

The ACR purge is designed to run as a container command in an ACR Task.

Based on the above command, it will remove all images created 0 days ago and keep 1. Keep 1 will hold the latest image in the repository.

  • filter: will filter the repository
  • ago: will define the duration

It is highly recommended to initiate the dry run before executing the purge action.

The result of the above command is from the Azure cloud shell.

Azure Cloud

Use the below command to permanently remove the images from the repository.

az acr run --registry <ACR Name> --cmd 'acr purge --filter <repository name>:.*  --ago 0d --untagged -keep 1' /dev/null

Docker Repository

From the above image, it is obvious that 1 old Manifest has been deleted, and we have only the latest image in the repository. 

Summary

We have seen how to delete the images from the Azure Container Registry using Azure CLI Command and how to initiate a dry run before executing an actual Purge command.