27

I have an image (in AWS ECR) with 2 tags and I would like to remove one tag only.

I can easily remove from my local environment using: docker rmi <REPOSITORY>:<TAG>

I would like to remove it from ECR. Any help would be appreciated.

Squirrel
  • 1,283
  • 1
  • 13
  • 22

3 Answers3

63

You can delete a tag using the following command:

aws ecr batch-delete-image --repository-name <REPO NAME> --image-ids imageTag=<TAG NAME>

If you have only one tag and execute this command, it will remove the image. If you have multiple tags for the same image, specify one, and only the tag is removed.

Cemal Unal
  • 514
  • 3
  • 6
  • 15
Squirrel
  • 1,283
  • 1
  • 13
  • 22
5

In case one needs to delete more tags, here is a an extension of the accepted answer:

ECR_REPO="my-repo-name"

# Create a file with the list of AWS ECR tags, with one tag per line
aws ecr list-images --repository-name $ECR_REPO --filter "tagStatus=TAGGED" \
    --query "imageIds[*]" --output text \
    | cut -f2 > ecr-image-tags-${ECR_REPO}.txt

# For each tag, delete the image on AWS ECR
cat ecr-image-tags-${ECR_REPO}.txt | xargs -I {} \
    aws ecr batch-delete-image --repository-name ${ECR_REPO} --image-ids imageTag={} | cat

You can also replace cat by grep -E PATTERN in the last line if you want to selectively delete tagged images with a specific pattern.

Pierre
  • 1,068
  • 1
  • 9
  • 13
  • batch-delete-image takes multiple arguments to `--image-ids`. you may want to batch these if you are deleting a lot of tags to avoid any resource throttling – spazm Jul 12 '21 at 18:30
1

We can improve on the batch solution from the "delete more tags" answer by using multiple --image-ids per call to `aws ecr batch-delete-image

Assuming a file ecr-image-tags-${ECR_REPO}.txt containing 1 tag per line to be removed (as per the other answer.) Don't forget to remove the tags from the file that you want to keep!

BATCH_SIZE=20  # set max number of tags per batch-delete-image call
< ecr-image-tags-${ECR_REPO}.txt sed -e 's/^/imageTag=/' | \
    xargs -n $BATCH_SIZE \
    aws ecr batch-delete-image --image-ids

Removing -n $BATCH_SIZE will put all the tags in a single call to batch-delete-image.

spazm
  • 4,399
  • 31
  • 30