Last active
May 13, 2021 17:32
-
-
Save GustavoAmerico/99a70a45a06d20feb0278b1a2e9787f6 to your computer and use it in GitHub Desktop.
Cleanup-Azure-Container-Registry.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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