Azure Container Registry - delete all images except 2

狂风中的少年 提交于 2019-12-11 05:11:59

问题


I want to delete all images in Azure Container Registry except the last two. I was looking for an script for do so but I only find to delete images older than X days. This is not possible for my situation because some days there are a lot of images created and other days only one.

Somebody has any idea?


回答1:


I am unable to test it right now but this little PowerShell script should work:

$acrName = 'YourACRName'

$repo = az acr repository list --name $acrName
$repo | Convertfrom-json | Foreach-Object {
    $imageName = $_
    (az acr repository show-tags -n $acrName --repository $_ | 
        convertfrom-json |) Select-Object -SkipLast 2 | Foreach-Object {
        az acr repository delete -n $acrName --image "$imageName:$_"
        }
}

It retrieves all tags for each repository, skips the last 2, then it iterates over each tag and deletes it.

Please test it in some kind of test environment first.



来源:https://stackoverflow.com/questions/57579940/azure-container-registry-delete-all-images-except-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!