0

I am managing a Azure Container Registry. I have scheduled a ACR Purge task which is deleting all image tag if they are older than 7 days and exclude versioned images which are starting with v so that we can skip certain image from cleanup.

For Example: if image has name like

123abc
v1.2
v1.3
xit5424
v1.4
34xyurc
v2.1

So it should delete images which are not starting with v and should delete the images which are not starting with v. For example it should delete below images-

123abc
xit5424
34xyurc

My script is something like this.

PURGE_CMD="acr purge --filter 'Repo1:.' --filter 'ubuntu:.' --ago 7d --untagged --keep 5"

az acr run --cmd "$PURGE_CMD" --registry Myregistry /dev/null

Thanks Ashish

Ashish Kumar
  • 173
  • 2
  • 13
  • Hi Ashish, you can use a regular expression after the image name as a filter, like: hello-world:^1.* You can try for yourself (with —dry-run) I’ll look at it on Wednesday. – Patrick Koorevaar Feb 28 '22 at 10:29

1 Answers1

2

Please check if below gives an idea to workaround :

Here I am trying to make use of delete command .

grep -v >>Invert the sense of matching, to select non-matching lines.

Grep -o >> Show only the part of a matching line that matches PATTERN. grep - Reference

1)Try to get the tags which does not match the pattern "v"

$tagsArray = az acr repository show-tags --name myacr --repository myrepo --orderby time_desc \
    --output tsv | grep -v "v"

Check with purge command below if possible (not tested)

PURGE_CMD="acr purge --filter 'Repo1:.' --filter 'ubuntu:.' --ago 7d --filter '$tagsArray' --untagged --keep 5"
az acr run --cmd "$PURGE_CMD" --registry Myregistry /dev/null

(or)

check by using delete command

Ex:

$repositoryList = (az acr repository list --name $registryName --output json | ConvertFrom-Json)

foreach ($repo in $repositoryList)
{
$tagsArray = az acr repository show-tags --name myacr --repository myrepo --orderby time_desc \
    --output tsv | grep -v "v"

    foreach($tag in $tagsArray)
    {
            az acr repository delete --name $registryName --image $repo":"$tag --yes
        
    }
}

Or we can get all tags with a query which should not be deleted and can use if else statement tag .

foreach ($repo in $repositoryList)
    {

  $AllTags = (az acr repository show-tags --name $registryName --repository $repo --orderby time_asc --output json | ConvertFrom-Json ) | Select-Object -SkipLast $skipLastTags


$doNotDeleteTags=$( az acr repository show-tags  --name $registryName --query "[?contains(name, 'tagname')]" --output tsv)

 #or     $doNotDeleteTags  = az acr repository show-tags --name $registryName --repository $repo --orderby time_asc --output json | ConvertFrom-Json )  -- query "[?starts_with(name,'prefix')].name" 


foreach($tag in $AllTags)
    {

        if ($donotdeletetags -contains $tag)
        {
            Write-Output ("This tag is not deleted $tag")
        }
        else
        {
            az acr repository delete --name $registryName --image $repo":"$tag --yes
        }
 
    }
   } 

References:

  1. fetch-the-latest-image-from-acr-that-doesnt-start-with-a-prefix
  2. azure-container-registry-delete
  3. how-to-delete-image-from-azure-container-registry
  4. acr-delete-only-old-images-
kavyaS
  • 8,026
  • 1
  • 7
  • 19