Skip to content

Instantly share code, notes, and snippets.

@thepaulmacca
Created July 22, 2022 22:53
Show Gist options
  • Save thepaulmacca/1abd09f38cc63f1dfccb95238e126b02 to your computer and use it in GitHub Desktop.
Save thepaulmacca/1abd09f38cc63f1dfccb95238e126b02 to your computer and use it in GitHub Desktop.
Azure Container Registry - Cleanup
# utils:
filter timestamp {"$(Get-Date -UFormat '%T') | $_"}
$acrName=''
$acrMaxImageTags=3
if ($(az acr list --query "[?name=='$acrName']" | ConvertFrom-Json).Length -gt 0) {
echo "Pruning old image tags from: $acrName" | timestamp
$repositories = az acr repository list --name $acrName --output tsv
foreach ($repository in $repositories) {
echo "Pruning repository $repository" | timestamp
$tags = az acr repository show-tags --name $acrName --repository $repository --output tsv --orderby time_desc | where {$_ -ne 'latest'}
if ($tags.length -gt $acrMaxImageTags) {
echo "Repository tag count: $($tags.length); max tags: $acrMaxImageTags; pruning excess"
for ($i=$acrMaxImageTags; $i -lt $tags.length; $i++) {
$imageName = "$($repository):$($tags[$i])"
echo "Deleting: $imageName" | timestamp
az acr repository delete --name $acrName --image $imageName --yes --only-show-errors
}
}
}
echo "Done pruning ACR $acrName" | timestamp
}
# credits:
# https://github.com/andrew-kelleher/azurecontainerregistry-cleanup/blob/master/acr-cleanup.ps1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment