azureazure-container-registrypurge

Azure Container Registery: How to clean all repositories


I want to clean azure container registry repositories with schedule. This shows how to filter and more but this is only for one repository: https://learn.microsoft.com/en-us/azure/container-registry/container-registry-auto-purge

Then this subject show how to clean multiple repositories: Ho can I purge all repository from Azure container registry without entering all repository one-by-ne

But mainly both of them doesnt conver for the future repositories. How can i set a task or something else that cleans repositories including future repositories with the filter set.


Solution

  • Take a look at this. You can use a script to loop over your repos and delete the dangling images:

    $ACRS = Get-AzContainerRegistry
    foreach ($ACR in $ACRS) {
      $REPOS = Get-AzContainerRegistryRepository -RegistryName $ACR.Name
      foreach ($REPO in $REPOS) {
        $MANIFESTS = (Get-AzContainerRegistryManifest -RegistryName $ACR.Name -RepositoryName $REPO).ManifestsAttributes | Where-Object { $_.Tags -eq $null } | Sort-Object -Property LastUpdateTime -Descending
        foreach ($ITEM in $MANIFESTS) {
          $TAG = $ITEM.digest
          Write-OutPut "------------------------"
          Write-Output "Delete dangling image $REPO@$TAG"
          Remove-AzContainerRegistryManifest -RegistryName $ACR.Name -RepositoryName $REPO -Manifest $TAG
        }
      }
    }
    

    This can be used ad-hoc (e.g., run this in your Azure shell) or in the pipeline.