Skip to content

Instantly share code, notes, and snippets.

@mlf4aiur
Last active November 18, 2016 02:13
Show Gist options
  • Select an option

  • Save mlf4aiur/2ab96ebedfb725dd19f5e7f70077bcdf to your computer and use it in GitHub Desktop.

Select an option

Save mlf4aiur/2ab96ebedfb725dd19f5e7f70077bcdf to your computer and use it in GitHub Desktop.
Cleanup AWS ECR untagged images
#!/bin/bash
function delete_image () {
local repo_name=$1
local image_digest=$2
echo "${image_digest}"
aws --out=json \
ecr \
batch-delete-image \
--repository-name="${repo_name}" \
--image-ids \
imageDigest="${image_digest}"
}
# Check requirements
function require {
command -v "$1" > /dev/null 2>&1 || {
echo "Some of the required software is not installed:"
echo " please install $1" >&2;
exit 1;
}
}
# Check for AWS, AWS Command Line Interface
require aws
# Check for jq, Command-line JSON processor
require jq
repositories=$(aws --out=json \
ecr \
describe-repositories | \
jq '.repositories[].repositoryName' | \
sed 's/"//g'
)
for repo_name in ${repositories}
do
echo "${repo_name}"
images=$(aws --out=json \
ecr \
list-images \
--repository-name="${repo_name}" | \
jq '.imageIds[] | select(.imageTag == null) | .imageDigest'
)
# alternative
# images=$(aws --out=json \
# ecr \
# list-images \
# --repository-name="${repo_name}" \
# --filter tagStatus=UNTAGGED | \
# jq '.imageIds[] | .imageDigest'
# )
for image_digest in ${images}
do
delete_image "${repo_name}" "${image_digest}"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment