2

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

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Den Wahlin
  • 55
  • 5

2 Answers2

8

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
Srijit_Bose-MSFT
  • 1,010
  • 4
  • 13
2

$PURGE_CMD="acr purge --filter '.*:.*' --ago 30d" command looks good for me. So, there are 2 expressions: for repo name and for tag name

  • Just used this command, works just as well, a lot easier then the accepted answer. And when you create a scheduled purge task using this syntax it will also include purge repositories that are created afterwards. – Evertvdw Apr 07 '23 at 11:36