-
-
Save bitnik/b59ae9a9d1d49545dcc8091513bef0d0 to your computer and use it in GitHub Desktop.
Delete Docker images on DockerHub
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 | |
# Based on kizbitz/dockerhub-v2-api-organization.sh at https://gist.github.com/kizbitz/175be06d0fbbb39bc9bfa6c0cb0d4721 | |
# Was also useful: https://success.docker.com/article/how-do-i-authenticate-with-the-v2-api | |
# 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 | |
UNAME="gesisnotebooks" | |
UPASS="" | |
ORG="gesiscss" | |
IMAGE_REGEX="orc-binder-*" | |
DONT_DELETE="orc-binder-gesiscss-2dflow-8e10b7|dont-delete-this-neither" | |
# ------- | |
set -e | |
# get token | |
echo "Retrieving token ..." | |
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' 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 | |
echo "Deleting images and tags for organization: ${ORG}" | |
for i in ${REPO_LIST} | |
do | |
# Delete all binder repos | |
if [[ ${i} == ${IMAGE_REGEX} ]] && [[ ! ${i} =~ ^(${DONT_DELETE})$ ]] | |
then | |
echo -n "${i}: " | |
# NOTE comment this line to test | |
curl -X DELETE -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/${i}/ | |
echo "DELETED" | |
fi | |
# Delete by tags (TODO: filter) | |
#IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/${i}/tags/?page_size=300 | jq -r '.results|.[]|.name') | |
#for j in ${IMAGE_TAGS} | |
#do | |
# echo -n " - ${j} ... " | |
# curl -X DELETE -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/${i}/tags/${j}/ | |
# echo "DELETED" | |
#done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment