Exclude certain tags while deleting repositories in Azure Container registry

135 views Asked by At

Is there a way we can exclude deleting certain tags from a Azure container repository and delete rest all tags present.

I have been trying with below command but doesn't seem to work

az acr run --registry <myRegistry> --cmd 'acr purge --filter "myRepo.^((?!tag_to_exlude).)*$”’ /dev/null
1

There are 1 answers

3
Imran On

There is no direct way to exclude certain tags from an Azure container registry and delete rest all tags present. The acr purge command does not support excluding tags.

However, there is a workaround you can use to achieve the same result:

# Define your Azure Container Registry name and the tag(s) you want to exclude
$registryName = "your-acr-name"
$tagsToExclude = "tag_to_exclude1", "tag_to_exclude2"

# Get a list of repository names in the ACR
$repositories = az acr repository list --name $registryName --output tsv

# Loop through each repository
foreach ($repository in $repositories) {
    # Get a list of all tags in the repository
    $tags = az acr repository show-tags --name $registryName --repository $repository --output tsv

    # Filter tags to exclude the specified ones
    $tagsToDelete = $tags | Where-Object { $tagsToExclude -notcontains $_ }

    # Delete the remaining tags
    foreach ($tag in $tagsToDelete) {
        $imageToDelete = "$($repository):$($tag)"
        az acr repository delete --name $registryName --image $imageToDelete --yes
    }
}

enter image description here

Here as you can see, except V3 I am deleting rest all version of my tag using the above script:

In my repro I had different tags except v3 I delete all like below:

enter image description here

After running the script rest all deleted only v3 is left like below:

enter image description here

In portal:

enter image description here