Skip to content

Instantly share code, notes, and snippets.

@karlskewes
Forked from jriguera/delete-dockerhub-images.sh
Last active November 13, 2018 20:40
Show Gist options
  • Save karlskewes/ab2d9cd3613c38e815ea2e2a180eaf46 to your computer and use it in GitHub Desktop.
Save karlskewes/ab2d9cd3613c38e815ea2e2a180eaf46 to your computer and use it in GitHub Desktop.
Delete Docker images on DockerHub
#!/bin/bash
# TODO: Tidy up! - Karl
# Based on kizbitz/dockerhub-v2-api-organization.sh at https://gist.github.com/kizbitz/175be06d0fbbb39bc9bfa6c0cb0d4721
# Example for the Docker Hub V2 API
# Returns all images and tags associated with a Docker Hub organization account.
# Requires 'jq': https://stedolan.github.io/jq/
# set username, password, and organization
username="changeme"
password='changeme'
org="changeme"
keep_tags="^v-.*|stable|docker|demo|latest"
delete_tags="^git-.*$|^[a-zA-Z0-9]{40}$"
# -------
# Safe shell scripts, exit if error, unset variables, disable file expansion globbing (may break things), exit if any failure.
set -euf -o pipefail
# get token
echo "Retrieving token ..."
token=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${username}'", "password": "'${password}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
# get list of repositories
echo "Retrieving repository list ..."
repo_list=$(curl -s -H "Authorization: JWT ${token}" "https://hub.docker.com/v2/repositories/${org}/?page_size=200" | jq -r '.results|.[]|.name')
# delete images and/or tags
for repo in ${repo_list}
do
echo "Retrieving images and tags for organization: ${org}/${repo}"
# Delete repo (all)
# curl -X DELETE -s -H "Authorization: JWT ${token}" "https://hub.docker.com/v2/repositories/${org}/${i}/"
# Delete by tags
# Retrieve list of tags - newest image first
image_tags=$(curl -s -H "Authorization: JWT ${token}" "https://hub.docker.com/v2/repositories/${org}/${repo}/tags/?page_size=300" | jq -r '.results|.[]|.name')
# echo "${image_tags}"
images_to_delete_count=0
images_to_keep_count=0
echo "Dry run check what images should keep or delete..."
# We should really build an array of image tags to delete here.
for image in ${image_tags}
do
echo -n "Checking image: ${image} ... "
if [[ ${image} =~ ^(${delete_tags})$ ]] && [[ ! ${image} =~ ^(${keep_tags})$ ]]
then
echo "delete"
images_to_delete_count=$((images_to_delete_count+1))
else
echo "keep"
images_to_keep_count=$((images_to_keep_count+1))
fi
done
if [[ ${images_to_delete_count} -eq 0 ]]
then
echo "Status: Keep: ${images_to_keep_count} images. Delete: ${images_to_delete_count} images. Moving on to next repo..."
else
echo "Status: Keep: ${images_to_keep_count} images. Delete: ${images_to_delete_count} images."
echo "Confirmation Required: Proceed with deleting ${images_to_delete_count} images marked 'delete'? y/n"
read -r proceed
if [[ ${proceed} == "y" ]]
then
for image in ${image_tags}
do
if [[ ${image} =~ ^(${delete_tags})$ ]] && [[ ! ${image} =~ ^(${keep_tags})$ ]]
then
echo -e "\nDeleting: https://hub.docker.com/v2/repositories/${org}/${repo}/tags/${image}/"
curl -X DELETE -s -H "Authorization: JWT ${token}" "https://hub.docker.com/v2/repositories/${org}/${repo}/tags/${image}/"
fi
done
fi
echo ""
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment