Eric
Eric

Reputation: 1232

Why were more than one image deleted from ACR?

I am trying to write a cleanup script. The script is supposed to delete old ACR images. Here the sh command that runs in Jenkins:

ACRS=( mydevacr )
for ACR in "\${ACRS[@]}"
do
    readarray -t repos < <(az acr repository list --name \$ACR | jq -cr '.[]')
    for imageName in "\${repos[@]}"; do
        readarray -t tags < <(az acr repository show-tags -n \$ACR --repository \$imageName --orderby time_desc | jq -cr '.[]')
        for tag in "\${tags[@]:30}"
        do
            az acr repository delete -n \$ACR --image "\$imageName:\$tag" --yes
        done
    done
done

It runs as expected for a while but then, as seen here, something unexplainable happens. Also, this runs as a Jenkins pipeline and so, I have to escape $ so that Groovy doesn't try to process them.

+ for tag in "${tags[@]:30}"
+ az acr repository delete -n mydevacr --image frontend-dev:606 --yes
WARNING: This operation will delete the manifest 'sha256:44355fdb6becffc44d2cc6a4f3d00d9d7963bf00fbe4375e' and all the following images: 'frontend-dev:606'
+ for tag in "${tags[@]:30}"
+ az acr repository delete -n mydevacr --image frontend-dev:605 --yes
WARNING: This operation will delete the manifest 'sha256:8e48ac6c8b51458b4846c10b01260d74cbb11ac3c7f52c5' and all the following images: 'frontend-dev:605'
+ for tag in "${tags[@]:30}"
+ az acr repository delete -n mydevacr --image frontend-dev:604 --yes
WARNING: This operation will delete the manifest 'sha256:2b443ff1da9604b4b4a3b1831095375b6fd72c19c39e2e93' 
and all the following images: 'frontend-dev:525', 'frontend-dev:526', 'frontend-dev:527', 
'frontend-dev:528', 'frontend-dev:529', 'frontend-dev:530', 'frontend-dev:531', 'frontend-dev:533', 
'frontend-dev:534', 'frontend-dev:535', 'frontend-dev:536', 'frontend-dev:537', 'frontend-dev:538', 
'frontend-dev:539', 'frontend-dev:540', 'frontend-dev:542', 'frontend-dev:545', 'frontend-dev:547', 
'frontend-dev:549', 'frontend-dev:550', 'frontend-dev:551', 'frontend-dev:555', 'frontend-dev:556', 
'frontend-dev:557', 'frontend-dev:558', 'frontend-dev:559', 'frontend-dev:560', 'frontend-dev:561', 
'frontend-dev:562', 'frontend-dev:563', 'frontend-dev:565', 'frontend-dev:566', 'frontend-dev:569', 
'frontend-dev:571', 'frontend-dev:572', 'frontend-dev:574', 'frontend-dev:575', 'frontend-dev:576', 
'frontend-dev:577', 'frontend-dev:579', 'frontend-dev:580', 'frontend-dev:581', 'frontend-dev:582', 
'frontend-dev:583', 'frontend-dev:584', 'frontend-dev:585', 'frontend-dev:586', 'frontend-dev:590', 
'frontend-dev:591', 'frontend-dev:592', 'frontend-dev:593', 'frontend-dev:594', 'frontend-dev:595', 
'frontend-dev:596', 'frontend-dev:597', 'frontend-dev:599', 'frontend-dev:600', 'frontend-dev:602', 
'frontend-dev:604'

The last az command az acr repository delete -n mydevacr --image frontend-dev:604 --yes deletes 59 images when only 1 is specified.

How is this possible?

I've 'anonymized' things here to not reveal too much of our setup.

And, since this runs in Jenkins Pipeline, I've had to escape $ signs so that Groovy doesn't try to process them.

Upvotes: 0

Views: 238

Answers (1)

Charles Xu
Charles Xu

Reputation: 31454

As I see about your script, you just list the images in the repository and then delete them, without any conditions.

I think what you need to do is to add the condition in the script. For example, you need to delete the five oldest images in the repository, you can get the images through the command:

az acr repository show-tags -n \$ACR --repository \$imageName --orderby time_asc --top 5

Note: the time is a thing that increases, so you need to order the images by time_asc. And this command only shows five images to you. You can get more images as you need.

And also, the command az acr repository delete will delete the manifest referenced by the image tag you used and all other tags referencing the same manifest. So when the manifest all deleted, then the command will not delete manifest again. I think it's the situation that you meet. So I recommend you just untag the images instead of deleting the images directly.

Upvotes: 1

Related Questions