I would like to know the CLI command to delete all images in an ECR repo.
3 Answers
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 Regionmy-ecr-repo
is my ECR repo name
References:

- 1,891
- 10
- 17
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

- 4,458
- 2
- 28
- 40
This Script is written to delete Docker images from Single or Multiple ECR Repos.
Save the updated script to a file (e.g., delete_ecr_images.sh),
make it executable (chmod +x delete_ecr_images.sh), and run it:
./delete_ecr.sh
it will ask for Enter ECR repository names (space-separated): please enter all ECR repo name seperating with single spaces.
#!/bin/bash
# Login to AWS ECR aws ecr get-login-password --region <AWS Region> | docker login --username AWS --password-stdin
<AWS Account ID>.dkr.ecr.<AWS Region>.amazonaws.com
# Read repository names from user input read -p "Enter ECR repository names (space-separated): " repository_names
# Split repository names into an array IFS=' ' read -ra repositories <<< "$repository_names"
# Function to delete images in a repository delete_images() { local repository="$1" echo "Deleting images in repository: $repository" local image_tags image_tags=$(aws ecr describe-images
--repository-name "$repository" --query 'imageDetails[].imageTags' --output json) if [[ -n "$image_tags" ]]; then
delete_image_tags=$(echo "$image_tags" | jq -r '.[][]')
total_images=$(echo "$image_tags" | jq -r '. | length')
current_image=1
for image_tag in $delete_image_tags; do
image_id=$(aws ecr list-images --repository-name "$repository" --filter "tagStatus=TAGGED" --query "imageIds[?imageTag=='$image_tag'].imageDigest" --output text)
aws ecr batch-delete-image --repository-name "$repository" --image-ids "imageDigest=$image_id" >/dev/null 2>&1
echo "Deleted image $current_image of $total_images in repository: $repository (Tag: $image_tag)"
((current_image++))
done
echo "Deleted all images in repository: $repository" else
echo "No images found in repository: $repository" fi }
# Iterate through repositories and delete images for repository in "${repositories[@]}"; do delete_images "$repository" done
<AWS Region>
is AWS Region.
<AWS Account ID>
is AWS ECR Account ID.
here batch-delete-image documentation https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ecr/batch-delete-image.html
The script will prompt you to enter the ECR repository names separated by spaces. It will then iterate through each repository, delete all the images, and display the progress of each image deletion, including the image tag.
Note: Docker daemon is running on the machine to login to AWS ECR.

- 382
- 2
- 7