Last active
December 19, 2024 17:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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