Azure container registry repository image custom cleanup

108 Views Asked by At

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")

2

There are 2 best solutions below

3
Jahnavi On BEST ANSWER

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.

0
Vowneee On

From @Jahnavi's solution provided, created a shell script as below to solve our requirement.

#!/bin/bash

reg='xxxx'
skipLastTags=xxx
repositories=$(az acr repository list --name $reg --output json | jq -r '.[]')

for repo in $repositories; do
    tags=$(az acr repository show-tags --name $reg --repository $repo --orderby time_asc --output json | jq -r '.[]' | head -n -"$skipLastTags")
    
    for tag in $tags; do
        az acr repository delete --name $reg --image "$repo:$tag" --yes
    done
done