Search code examples
bashshellazure-cliazure-container-registry

Azure container registry repository image custom cleanup


Is there any way to enable retention policy for Azure ACR repositories, where only last 3 tags is need to keep and rest can be deleted. Also we have to exclude certain images from these repositories ( can be filtered with regex filter, eg: images have prefix "base-image")


Solution

  • You can use delete operation to delete the tags prior.

    Acr Delete tag

    Check whether the below Azure CLI script provides insights to meet your functionality.

    $reg = 'xxxx'
    $skipLastTags = 3
    $repositories = (az acr repository list --name $reg --output json | ConvertFrom-Json)
    foreach ($repo in $repositories)
    {
        $tags = (az acr repository show-tags --name $reg --repository $repo --orderby time_asc --output json | ConvertFrom-Json ) | Select-Object -SkipLast $skipLastTags
        foreach($tag in $tags)
        {
                az acr repository delete --name $reg --image $repo":"$tag --yes
     
        }
    }
    

    Refer SO for more relevant information on your requirement.