none
ACR - delete only old images RRS feed

  • Question

  • Hello,

    Im trying to cleanup old images in my ACR. It has 8 repositories so first I want it to test it in only one of them... The complicated thing about it that I need to keep last 4 images created. So I have this script:

    $acrName = ACRttestt

    $repo = az acr repository list --name $acrName --top 1

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


    But Im receiving the following error:

    Failed At line:9 char:58 + ... az acr repository delete -n $acrName --image "$imageName:$_" + ~~~~~~~~~~~ Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.

    Any ideas?

    Thanks in advance
    Wednesday, September 11, 2019 3:18 PM

Answers

  • Right. That would be this section of the code

    Write-Host "Checking registry: $AzureRegistryName"
    $RepoList = az acr repository list --name $AzureRegistryName --output table
    for($index=2; $index -lt $RepoList.length; $index++){
        $RepositoryName = $RepoList[$index]
    
        Write-Host "Checking for repository: $RepositoryName"
        $RepositoryTags = az acr repository show-tags --name $AzureRegistryName --repository $RepositoryName --orderby time_desc --output tsv
    
        # Delete by count if user specified a $NoOfKeptImages
        if ($NoOfKeptImages -gt 0){
            #since the list is ordered, delete the last X items
            foreach($tag in $RepositoryTags){
                if($RepositoryTags.IndexOf($tag) -ge $NoOfKeptImages){
                    $ImageName = $RepositoryName + ":" + $tag
                    Remove-Image -registryName $RepositoryName -imageName $ImageName -dryRun $DryRun

    However if you modify it to something like

    $AzureRegistryName = "NAMEHERE"
    
    Write-Host "Checking registry: $AzureRegistryName"
    $RepoList = az acr repository list --name $AzureRegistryName --output table
    
    
        Write-Host "Checking for repository: $RepositoryName"
        $RepositoryTags = az acr repository show-tags --name $AzureRegistryName --repository $RepositoryName --orderby time_desc --output tsv
    
        # Delete by count if user specified a $NoOfKeptImages
        if ($NoOfKeptImages -gt 0){
            #since the list is ordered, delete the last X items
            foreach($tag in $RepositoryTags){
                if($RepositoryTags.IndexOf($tag) -ge $NoOfKeptImages){
                    $ImageName = $RepositoryName + ":" + $tag
                    Remove-Image -registryName $RepositoryName -imageName $ImageName -dryRun $DryRun

    Essentially just set the ACR name and remove the for loop instead of it checking all the ARCs

    • Marked as answer by exitista Monday, September 16, 2019 7:10 PM
    Thursday, September 12, 2019 8:10 PM
    Owner

All replies

  • Just curious, why not just delete the old images using the portal to ensure you keep the latest images? 
    Wednesday, September 11, 2019 4:09 PM
    Owner
  • Because it must be an automatic process. I have 8 repositories and if not I have to connect every week and delete bymyself

    Thursday, September 12, 2019 1:41 PM
  • Got it. That makes sense :) 

    Check out this: https://github.com/goyalmohit/acr-cleanup

    Looking it over it seems this would get you close to what you are trying to do. Might need to modify it a bit to get the last 4 images but regardless this appears to be a good solution. 

    Thursday, September 12, 2019 7:27 PM
    Owner
  • Yes, I saw that script but you cannot set the repository. 

    The script run in all ACR repositories. I want to do it in only one of it

    Any idea?

    Thanks in advance

    Thursday, September 12, 2019 8:01 PM
  • Right. That would be this section of the code

    Write-Host "Checking registry: $AzureRegistryName"
    $RepoList = az acr repository list --name $AzureRegistryName --output table
    for($index=2; $index -lt $RepoList.length; $index++){
        $RepositoryName = $RepoList[$index]
    
        Write-Host "Checking for repository: $RepositoryName"
        $RepositoryTags = az acr repository show-tags --name $AzureRegistryName --repository $RepositoryName --orderby time_desc --output tsv
    
        # Delete by count if user specified a $NoOfKeptImages
        if ($NoOfKeptImages -gt 0){
            #since the list is ordered, delete the last X items
            foreach($tag in $RepositoryTags){
                if($RepositoryTags.IndexOf($tag) -ge $NoOfKeptImages){
                    $ImageName = $RepositoryName + ":" + $tag
                    Remove-Image -registryName $RepositoryName -imageName $ImageName -dryRun $DryRun

    However if you modify it to something like

    $AzureRegistryName = "NAMEHERE"
    
    Write-Host "Checking registry: $AzureRegistryName"
    $RepoList = az acr repository list --name $AzureRegistryName --output table
    
    
        Write-Host "Checking for repository: $RepositoryName"
        $RepositoryTags = az acr repository show-tags --name $AzureRegistryName --repository $RepositoryName --orderby time_desc --output tsv
    
        # Delete by count if user specified a $NoOfKeptImages
        if ($NoOfKeptImages -gt 0){
            #since the list is ordered, delete the last X items
            foreach($tag in $RepositoryTags){
                if($RepositoryTags.IndexOf($tag) -ge $NoOfKeptImages){
                    $ImageName = $RepositoryName + ":" + $tag
                    Remove-Image -registryName $RepositoryName -imageName $ImageName -dryRun $DryRun

    Essentially just set the ACR name and remove the for loop instead of it checking all the ARCs

    • Marked as answer by exitista Monday, September 16, 2019 7:10 PM
    Thursday, September 12, 2019 8:10 PM
    Owner
  • OK I will try!

    Do you know which modules do I have to import to run it as a runbook in Azure?

    Thanks in advance

    Monday, September 16, 2019 4:54 PM
  • I don't know the exact ones you might need. However looking up the container modules I see a few you might want to import

    https://www.powershellgallery.com/packages?q=Azure+Container

    Monday, September 16, 2019 5:57 PM
    Owner
  • Ok thanks! I will close this question

    I will open another one because I dont find the module for this cmdlets


    Monday, September 16, 2019 7:12 PM