Reading Time: 5 minutes
Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email

As I work more with Containers and Azure Container Registry the more potential I see. In fact just the other day I was noticing that the list of images in my ACR was growing quickly. The list was getting huge. Now, this might be OK for some people, but I know I will never need some of the older images. So I thought there must be a way to automatically purge old images using ACR Tasks because if you have read my blog post on ACR Tasks https://pixelrobots.co.uk/2019/06/automatically-create-container-images-using-azure-container-registry-tasks/ you can build images automatically, so why can’t you purge them.

Thanks to the ACR CLI we are able to schedule a purge task to clean up old images. You will need version 2.0.69 or later of the Azure CLI to be able to purge your containers.

Below I show you how to do this.

Warning: This is currently in preview and may change.

A little bit about the purge command

First, lets have a look at the acr purge command. Currently, the command is distributed via a public container image (mcr.microsoft.com/acr/acr-cli:0.1), which is built from the source code in the acr-cli repo on GitHub. https://github.com/Azure/acr-cli

Basically, the acr purge command deletes images by tag in a repository that matches a name supplied via the –filter parameter and that are older than a specified duration using the –ago parameter as long as the image tag or repository has the write-enabled attribute set to true.

At a minimum, you should always supply the following two parameters when you run the acr purge command.

  • –filter This option allows you to specify what repository to purge and filter by tags. For example, if you wanted to filter all tags you would add –filter “repositoryname:.*”. You can also use regular expressions to be more concise. An example of using regular expressions would be something like –filter “repositoryname:^1.*”. This would match any tags beginning with 1.  to purge multiple repositories you can pass multiple –filter parameters.
  • –ago This option allows you to set a duration in days, hours or minutes beyond which images are deleted. A couple of examples would be –ago 7d6h30m This would delete any image older than 7 days 6 hours and 30 minutes old. You could also just use hours. –ago 2.5h this would delete any image older than 2.5 hours.

A couple of optional parameters that i like to use are following.

  • –dry-run This one is quite obvious, it will run the task but will not actually delete anything. You will see the same output that you would see if you did not use this option. I recommend you always run this before you take the plunge and purge everything.
  • –untagged This option is a nice way to delete any manifests that do not have tags.

Lets run it on demand

OK, so lets see what this command actually looks like.

The above command will allow you to purge on command. It has an environment variable that is used as the container start command. You will need to change that to suit your needs. As you can see I have the –dry-run option in. You will need to remove that when you are happy with the command.

Next, you have the az acr run command that actually starts the acr CLI container in your container registry and runs the command. Here you will need to add your registry name.

Now, lets run it and see what happens. You will need to connect to your Azure subscription using the az login command.

As you can see from the image above, a container is launched based in the acr cli image I talked about before. Then the command you added in the environment variable is run and the output is displayed in the terminal window.

You will see that the number of tags and manifests that will be deleted if I did not use the –dry-run parameter is 4.

This is a nice easy way to clean up your container registries on-demand, but what about on a schedule? Azure has your back on that.

On a schedule you say?

This is nice and easy, you can basically use the same command as above but you will need to add in the –name and –schedule parameter. This uses something called Cron expressions you can read more about them at https://docs.microsoft.com/en-us/azure/container-registry/container-registry-tasks-scheduled#cron-expressions.

For this example, I am going to keep things easy and get the task to run every day at 6:30 am UTC time.

Now, lets run this and see what happens.

Oh, some nice JSON. Lets check to see if the task is configured by using the az acr task list command.

In the above image, you can see the task you have just created. In this example, it is called purgeTask and it is a TIMER trigger.

Things you should know

What happens if you have lots and lots of images

If you have a lot of images to clean up then you may find that you need to use the –timeout parameter under the az acr run command. By default, for on-demand tasks, the timeout is 600 seconds. For scheduled tasks, it is 3600 seconds.

Below is an example of an on-demand task with the timeout option added.

Did you mention multiple repositories before, how?

As mentioned above you can also purge multiple repositories in your container registry. Its really easy to do, all you have to do is add another –filter option.

Below is an example of an on-demand task to clean up two repositories.

And there you have it

Above I have shown you how you can use Azure Container Registry Tasks to purge your repositories either on-demand or on a schedule. When you are happy with your tasks just remove the –dry-run to actually delete the images. If you would like to find out more about the acr purge command you can see the help file by using the following command.

I hope you have found this article helpful. If you have any questions or comments please leave them below or reach out via social media.

Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email

Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *