- Newest
- Most votes
- Most comments
Hello.
ECR has a lifecycle setting, so why not try deleting it automatically using this setting?
https://aws.amazon.com/jp/blogs/compute/clean-up-your-container-images-with-amazon-ecr-lifecycle-policies/
Alternatively, it can be deleted using a script using AWS CLI as shown below.
aws ecr describe-images --repository-name "riku-kobayashi" --query 'imageDetails[]' | jq 'sort_by(.imagePushedAt) | .[].imageTags[]' -r | head -n 176 | xargs -I{} aws ecr batch-delete-image --repository-name "riku-kobayashi" --image-ids imageTag={}
I found this script and it worked for me: https://gist.github.com/karl-cardenas-coding/a35c2a75296f939542796175348b938c
#!/bin/bash
export AWS_PAGER=""
delete_repo() {
local ecr_type=$1
local repo=$2
echo "Deleting images in $ecr_type repository: $repo"
images=$(aws $ecr_type describe-images --repository-name $repo --query 'imageDetails[*].[imageDigest]' --output text)
for image in $images; do
aws $ecr_type batch-delete-image --repository-name $repo --image-ids imageDigest=$image
done
echo "Deleting $ecr_type repository: $repo"
aws $ecr_type delete-repository --repository-name $repo --force
}
# Check command-line arguments
if [[ -z $1 ]]; then
echo "Usage: $0 <public|private|both>"
exit 1
fi
choice=$1
if [[ $choice == "private" || $choice == "both" ]]; then
private_repositories=$(aws ecr describe-repositories --query 'repositories[*].repositoryName' --output text)
for repo in $private_repositories; do
delete_repo "ecr" "$repo"
done
fi
if [[ $choice == "public" || $choice == "both" ]]; then
public_repositories=$(aws ecr-public describe-repositories --query 'repositories[*].repositoryName' --output text)
for repo in $public_repositories; do
delete_repo "ecr-public" "$repo"
done
fi
To delete all images in your Amazon ECR (Elastic Container Registry) private repositories, you can use the AWS CLI command aws ecr batch-delete-image. However, since you have 176 repositories, it's more efficient to use a script to automate the process
- Create a file named delete_ecr_images.sh with the following content:
#!/bin/bash
# List all repositories
repos=$(aws ecr describe-repositories --query 'repositories[].repositoryName' --output text)
# Loop through each repository
for repo in $repos; do
# List all image IDs in the repository
image_ids=$(aws ecr list-images --repository-name $repo --query 'imageIds[].imageDigest' --output text)
# Delete all images in the repository
aws ecr batch-delete-image --repository-name $repo --image-ids $image_ids
Run the script:
- Make the script executable: chmod +x delete_ecr_images.sh
- Run the script: ./delete_ecr_images.sh
Thank you! This is what I was looking for. But I think something is missing because I get this error message when I run the script: ./delete_ecr_images.sh: line 13: syntax error: unexpected end of file
I added "Done" to Line 13 but now I get: Error parsing parameter '--image-ids': Expected: '=', received: 'EOF' for input: sha256:3f5ba626a1e26856071363c5504b5d89dace55ea3f1dd33bf3bbd8db6829acfa
Hello,
To delete everything in your private repositories in Amazon ECR, you can use ECR's lifecycle policies or AWS CLI.
Option 1: ECR Lifecycle Policies
- You can set up a lifecycle policy in ECR to automatically delete old images based on your rules like delete images older than 30 days.
Option 2: AWS CLI Script
- Run the following command to delete all images in your repository:
aws ecr describe-images --repository-name "your-repository-name" --query 'imageDetails[].imageDigest' --output text | xargs -n1 -I {} aws ecr batch-delete-image --repository-name "your-repository-name" --image-ids imageDigest={}
This script lists all image digests in the repository and deletes them one by one. Replace "your-repository-name" with your actual repository name.
Have you tried running the script in your environment before posting your answer? The script you created cannot delete the image in ECR with the error below.
aws ecr describe-images --repository-name "kobayashi" --query 'imageDetails[].imageDigest' --output text | xargs -n1 -I {} aws ecr batch-delete-image --repository-name "kobayashi" --image-ids imageDigest={} xargs: warning: options --max-args and --replace/-I/-i are mutually exclusive, ignoring previous --max-args value { "imageIds": [], "failures": [ { "imageId": { "imageDigest": "sha256:506f330a66a341888e5c3fbcff9f97d2b1f19a31a5c359f766bcc70a4d7bd39a\tsha256:127262f8c4c716652d0e7863bba3b8c45bc9214a57d13786c854272102f7c945" }, "failureCode": "InvalidImageDigest", "failureReason": "Invalid request parameters: image digest should satisfy the regex '[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+'" } ] }Hi, Thank you! I didn't know there was a lifecycle setting. But it seems it's per repository, and I can't set it for everyone.
The AWS CLI is good if you want to delete one repository, but I got over 100 of them so I would rather not re-run that command over 100 times
Relevant content
- asked a year ago

Hi, Thank you! I didn't know there was a lifecycle setting. But it seems it's per repository, and I can't set it for everyone.
The AWS CLI is good if you want to delete one repository, but I got over 100 of them so I would rather not re-run that command over 100 times
Thank you! That works too :)
Not only the container image but also the ECR repository will disappear, but you can delete all repositories with the script below.