azurecontainersazure-container-registryacr

Ho can I purge all repository from Azure container registry without entering all repository one-by-ne


I am trying to clean few Azure container registry images from all repository. For now I am entering repository name one-by-one but this looks vary tedious like this.

PURGE_CMD="acr purge --filter 'aag:.*' --filter 'aap_6.0:.*' --ago 1d --untagged --keep 7"

  az acr task create --name PurgeACRImages \
  --cmd "$PURGE_CMD" \
  --schedule "5 6 * * 1" \
  --registry myregistry \
  --timeout 18000 \
  --context /dev/null

Here I have given name of two repository, now I have 800 repository and it's really hard to type 800 repository in this PURGE_CMD command.

I need a command through which I can select all repo from ACR and store into PURGE_CMD


Solution

  • We can loop on the output of az acr repository list to concatenate repository name filters to the PURGE_CMD variable.

    If you are using a Linux client, run the following from the command line:

    PURGE_CMD="acr purge --ago 1d --untagged --keep 7"
    for i in $(az acr repository list -n <yourRegistryName> -o tsv);do PURGE_CMD+=" --filter '"$i":.'";done
    echo $PURGE_CMD
    

    If you are using Windows Powershell run the following from the command line:

    $PURGE_CMD="acr purge --ago 1d --untagged --keep 7"
     foreach ($i in $(az acr repository list -n <yourRegistryName> -o tsv)){$PURGE_CMD+=" --filter '"+$i+":.'"}
     echo $PURGE_CMD
    

    Note: replace <yourRegistryName> with the name of your Azure Container Registry.

    Finally you can run:

    az acr task create --name PurgeACRImages
    --cmd "$PURGE_CMD"
    --schedule "5 6 * * 1"
    --registry myregistry
    --timeout 18000
    --context /dev/null