Skip to content

Instantly share code, notes, and snippets.

@karl-cardenas-coding
Last active October 11, 2023 15:45
Show Gist options
  • Save karl-cardenas-coding/a35c2a75296f939542796175348b938c to your computer and use it in GitHub Desktop.
Save karl-cardenas-coding/a35c2a75296f939542796175348b938c to your computer and use it in GitHub Desktop.
Remove all ECR repositories and images. Be aware that this removes ALL ECR repositories, depending on the choice of public | private | both.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment