Skip to content

Instantly share code, notes, and snippets.

@GustavoAmerico
Last active May 13, 2021 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GustavoAmerico/99a70a45a06d20feb0278b1a2e9787f6 to your computer and use it in GitHub Desktop.
Save GustavoAmerico/99a70a45a06d20feb0278b1a2e9787f6 to your computer and use it in GitHub Desktop.
Cleanup-Azure-Container-Registry.ps1
param(
[Parameter(Mandatory=$true)] $acrAccountName,
$takeLastImages = 5,
$likeRepositories = '',
$selectTagPattern = '^([0-9]+(\.)?){3,4}$'
)
#Get all repository where the name like with parameter $likeRepositories
$repositories = az acr repository list --name $acrAccountName --query ("[?contains(@, '" + $likeRepositories + "')]") | ConvertFrom-Json ;
#Get all image tags for repositories
$allTagsObjects = ( $repositories |% { az acr repository show-tags -n $acrAccountName --repository $_ --orderby time_desc --output 'json' --detail --query "map(&{ID: @.digest, Tag: @.name, repository: '$_' },@)" | Join-String | ConvertFrom-Json | Select-Object -Skip $takeLastImages } )
# Filtering the images ID with only one tag
$uniqueTags = $allTagsObjects | Where -FilterScript { $id=$_.ID;($allTagsObjects | Where-Object ID -EQ $id).Count -EQ 1 } | Where-Object Tag -Match $selectTagPattern
$uniqueTags | %{ $imageToRemove = ($_.repository + ":" + $_.Tag); write-host $imageToRemove; az acr repository delete -n $acrAccountName --image $imageToRemove -y }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment