Home > other >  How to delete all images in an ECR repository?
How to delete all images in an ECR repository?

Time:08-11

I would like to know the CLI command to delete all images in an ECR repo.

CodePudding user response:

According Amazon ECR Documentation - To delete an image (AWS CLI):

You can list the all images in your repository. Using this command line:

aws ecr list-images --repository-name my-repo

Then, you can iterate to delete all images using this command

aws ecr batch-delete-image \
     --repository-name my-repo \
     --image-ids imageTag=tag1 imageTag=tag2

Or to delete multiple images, you can specify multiple image tags or image digests in the request.

aws ecr batch-delete-image \
     --repository-name my-repo \
     --image-ids imageDigest=sha256:4f70ef7a4d29e8c0c302b13e25962d8f7a0bd304EXAMPLE imageDigest=sha256:f5t0e245ssffc302b13e25962d8f7a0bd304EXAMPLE

CodePudding user response:

Here is a bash script (delete.sh) that you can delete any images from your ECR repository:

#!/bin/bash
aws ecr batch-delete-image --region $1 \
    --repository-name $2 \
    --image-ids "$(aws ecr list-images --region $1 --repository-name $2 --query 'imageIds[*]' --output json
)" || true

You can execute by a single command like this:

./delete.sh ap-southeast-1 my-ecr-repo

with the following values:

  • ap-southeast-1 is my AWS Region
  • my-ecr-repo is my ECR repo name

References:

  • Related