Cleaning Up Your Azure Container Registry: Deleting Untagged Images in One Command
Maintaining a clean and efficient Azure Container Registry (ACR) is crucial for smooth development workflows. Over time, your repository can accumulate untagged images, leading to wasted storage space and potential confusion. Manually deleting these images can be tedious, especially for large repositories.
Fortunately, the Azure CLI provides a powerful solution to streamline this process: deleting all untagged images within a repository with a single command.
Scenario: Imagine you have a repository named "my-app-repo" in your ACR "my-acr" that's filled with untagged images taking up valuable space. You want to remove these images efficiently.
Original Code:
az acr image list -r my-acr/my-app-repo --output table
This command lists all images within the repository, including tagged and untagged ones. However, it doesn't directly delete them.
The Solution: Using az acr image delete
The key to deleting untagged images lies in the az acr image delete
command. This command allows you to specify the image to delete based on its digest. Let's break down how to achieve this in one command:
-
Get all image digests:
az acr image list -r my-acr/my-app-repo --output table | grep -v "IMAGE_TAG" | awk '{print $3}'
This command retrieves all images, filters out tagged images (those with an
IMAGE_TAG
column), and extracts the digest column (column 3). -
Delete images based on digests:
az acr image list -r my-acr/my-app-repo --output table | grep -v "IMAGE_TAG" | awk '{print $3}' | xargs -n 1 az acr image delete -r my-acr/my-app-repo --digest
This command takes the digests from the previous step, uses
xargs
to pass them one at a time to theaz acr image delete
command, and deletes each untagged image.
Explanation:
az acr image list
: Lists images in the repository.-r my-acr/my-app-repo
: Specifies the repository.--output table
: Outputs the results in a table format.grep -v "IMAGE_TAG"
: Filters out lines containing "IMAGE_TAG", removing tagged images.awk '{print $3}'
: Extracts the third column, which contains the image digest.xargs -n 1
: Passes each digest to the following command as a separate argument.az acr image delete
: Deletes the specified image.--digest
: Indicates that the next argument is the image digest.
Important Note: This command permanently deletes the untagged images. Make sure you have a backup or understand the implications before executing it.
Benefits of Using This Command:
- Efficient Cleanup: Saves time and effort compared to manual deletion.
- Automated Process: Can be easily incorporated into scripts or CI/CD pipelines for routine maintenance.
- Improved Repository Health: Ensures a lean and efficient repository by removing unnecessary images.
Additional Tips:
- Verify Image Count: Before deleting images, verify the number of untagged images using the
az acr image list
command. - Use a Script: Create a script that combines the commands for easier execution.
- Consider Image Retention Policies: Implement image retention policies to automatically delete old or untagged images.
Resources:
By utilizing this single command, you can quickly and effectively remove untagged images from your Azure Container Registry, freeing up storage space and ensuring a cleaner and more organized repository.