4

How to delete docker images from docker hub using a shell script.

I want to delete all images from docker hub older than past 50 days of a private docker hub account.

Any ideas? Which tools to use while creating and running this shell script from either through Jenkins or bash terminal.

I'm thinking of using curl to achieve my goal.

Dave M
  • 4,494
  • 21
  • 30
  • 30
amit
  • 61
  • 1
  • 4
  • HUB_TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${HUB_USERNAME}'", "password": "'${HUB_PASSWORD}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token) echo $HUB_TOKEN – amit Jun 18 '20 at 06:26
  • curl -i -X DELETE \ -H "Accept: application/json" \ -H "Authorization: JWT $HUB_TOKEN" \ https://hub.docker.com/v2/repositories///tags/latest i have above two commands to delete a image of "myes" repository in my docker hub but that image didn't got deleted from my docker hub and http response was "HTTP/1.1 301 MOVED PERMANENTLY". Is this syntax correct to delete a image or what else needs to be done to get the result. – amit Jun 18 '20 at 06:33
  • curl -si -L -X DELETE -H "Authorization: JWT ${HUB_TOKEN}" https://hub.docker.com/v2/repositories///tags/ this command did the trick for me – amit Jun 18 '20 at 16:20
  • We can also used the shell-script in link below and modified it to our purpose. https://success.docker.com/article/how-do-i-authenticate-with-the-v2-api – amit Jun 21 '20 at 05:06

3 Answers3

2

Below script will delete all images in all repositories of your docker hub account which are older than 50 days.


#!/bin/bash
#Script will delete all images in all repositories of your docker hub account which are older than 50 days

set -e

# set username and password
UNAME="YOUR_USERNAME"
UPASS="YOUR_PASSWORD"

# get token to be able to talk to Docker Hub
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 namespaces accessible by user (not in use right now)
#NAMESPACES=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/namespaces/ | jq -r '.namespaces|.[]')

#echo $TOKEN
echo
# get list of repos for that user account
echo "List of Repositories in ${UNAME} Docker Hub account"
sleep 5
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000 | jq -r '.results|.[]|.name')
echo $REPO_LIST
echo
# build a list of all images & tags
for i in ${REPO_LIST}
do
  # get tags for repo
  IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')

  # build a list of images from tags
  for j in ${IMAGE_TAGS}
  do
    # add each tag to list
    FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
      
  done
done

# output list of all docker images
echo
echo "List of all docker images in ${UNAME} Docker Hub account"
sleep 10
for i in ${FULL_IMAGE_LIST}
do
  echo ${i}
done

sleep 10
echo
echo "Identifying and deleting images which are older than 50 days in ${UNAME} docker hub account"
sleep 10
# Note!!! Please un-comment below line if you wanna perform operation on all repositories of your Docker Hub account
#for i in ${REPO_LIST}

for i in randomRepo

#NOTE!!! For deleting Specific repositories images please include only those repositories in for loop  like below for loop which has repos mygninx and mykibana 
#for i in  mynginx mykibana 

do
  # get tags for repo
  echo
  echo "Looping Through $i repository in ${UNAME} account"
  IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')

  # build a list of images from tags
  for j in ${IMAGE_TAGS}
  do
      echo
      # add last_updated_time
    updated_time=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/?page_size=10000 | jq -r '.last_updated')
    echo $updated_time
    datetime=$updated_time
    timeago='50 days ago'

    dtSec=$(date --date "$datetime" +'%s')
    taSec=$(date --date "$timeago" +'%s')

    echo "INFO: dtSec=$dtSec, taSec=$taSec" 

           if [ $dtSec -lt $taSec ] 
           then
              echo "This image ${UNAME}/${i}:${j} is older than 50 days, deleting this  image"
              ## Please uncomment below line to delete docker hub images of docker hub repositories
              #curl -s  -X DELETE  -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/
           else
              echo "This image ${UNAME}/${i}:${j} is within 50 days time range, keep this image"
           fi      
  done
done

echo "Script execution ends"



amit
  • 61
  • 1
  • 4
1
#!/bin/bash
#Script will delete all images in all repositories of your docker hub account which are older than 'X' days
set -e

# set your username, password and no. of 'X' days value in below lines.
#UNAME="YOUR_USERNAME"
#UPASS="YOUR_PASSWORD"
#X="YOUR_DAYS_VALUE"

# pass username,password and no of 'X' days value from terminal as below line.
# ./docker-images-remove-script.sh <username> <password> <30>
UNAME=$1
UPASS=$2
X=$3

# get token to be able to talk to Docker Hub
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)


#echo $TOKEN
echo
# get list of repos for that user account
echo "List of Repositories in '${UNAME}' Docker Hub account."
sleep 5
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000" | jq -r '.results|.[]|.name')
#echo "$REPO_LIST"
count=1
for rep in ${REPO_LIST}
do
     echo S.No: $count RepoName:  $rep
     count=`expr $count + 1`
done
echo
sleep 5



echo
echo "Identifying and deleting images which are older than $X days in '${UNAME}' docker hub account."
sleep 5

#NOTE!!! For deleting specific repositories images please include only those repositories in for-loop, like below for-loop which has repos mysql and mymongo 
#for i in  mysql mymongo

for rep in ${REPO_LIST}
 

do
    # get total no. of images & their count for a repo
    Images=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/$UNAME/$rep/tags/")
    ImageCount=$(echo $Images | jq -r '.count')    
    echo "Total no of Images in '$UNAME/$rep' repository are: $ImageCount"
    pages=`expr $ImageCount / 100 + 1`
    echo "No pages to iterate are: $pages"    
    sleep 5
    for (( p=1; p<=$pages; p++ ))
    do         
        echo "Looping Through '$rep' repository in '${UNAME}' account."
        IMAGES=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${rep}/tags/?page_size=100&page=$p") 
        IMAGE_TAGS=$(echo $IMAGES | jq -r '.results|.[]|.name')
        count1=1

             # build a list of images from tags
             for tag in ${IMAGE_TAGS}
             do
                  
                  echo Iteration no. is: $p
                  echo "S.No: $count1. RepoName: '$rep' ImageTag: $tag"
                  count1=`expr $count1 + 1`
                  sleep 5
                  # Get last_updated_time
                  updated_time=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${rep}/tags/${tag}/?page_size=100 | jq -r '.last_updated')
                  echo "Image build date and time is : $updated_time"
                  datetime=$updated_time
                  timeago=''$X' days ago'
                  #echo $timeago

                  dtSec=$(date --date "$datetime" +"%Y%m%d")
                  taSec=$(date --date "$timeago"  +"%Y%m%d")
                   
                  dt_Sec=$(date --date "$datetime" +"%Y-%m-%d")
                  ta_Sec=$(date --date "$timeago"  +"%Y-%m-%d")
                  

                  echo "INFO: Date on which this image was build: $dt_Sec" 
                  echo "INFO: $X days earlier date from today is: $ta_Sec" 
                  sleep 5
                  if [ $dtSec -lt $taSec ] 
                  then
                        echo "This image '${UNAME}/${rep}:${tag}'  is older than $X days, deleting this  image."
                  #### Note! TO delete an image please uncomment below line.
                  #### curl -s  -X DELETE  -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${rep}/tags/${tag}/
                  else
                        echo "This image '${UNAME}/${rep}:${tag}' is within $X days time range, keeping this image."
                  fi
                  echo
             done      
    done
echo
done

echo "Script execution ends here."

arvind
  • 11
  • 2
0

My version for a specific repository in a specific organization.

You should tweak the pagination ('?page=2000') to grab the old images you are interested in.

Then run it in a bash loop.

while true; do bash delete_old_images.sh; done

./delete_old_images.sh

#!/bin/bash
#Script will delete all images in all repositories of your docker hub account which are older than 50 days

set -e

UNAME="user"
UPASS="password"
ORGANIZATIONNAME="user"
REPOSITORY="repo"

# get token to be able to talk to Docker Hub
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)

echo
echo "Identifying and deleting images which are older than 50 days in ${ORGANIZATIONNAME} docker hub account"

# get tags for repo
echo
echo "Looping Through ${REPOSITORY} repository in ${UNAME} account"
IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORGANIZATIONNAME}/${REPOSITORY}/tags/?'page=2000' | jq -r '.results|.[]|.name')

# build a list of images from tags
for j in ${IMAGE_TAGS}
do
    echo
    # add last_updated_time
  updated_time=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORGANIZATIONNAME}/${REPOSITORY}/tags/${j}/?page_size=10000 | jq -r '.last_updated')
  echo $updated_time
  datetime=$updated_time
  timeago='50 days ago'

  dtSec=$(date --date "$datetime" +'%s')
  taSec=$(date --date "$timeago" +'%s')

  echo "INFO: dtSec=$dtSec, taSec=$taSec"

  if [ $dtSec -lt $taSec ]; then
    echo "This image ${UNAME}/${REPOSITORY}:${j} is older than 50 days, deleting this image"
    ## Please uncomment below line to delete docker hub images of docker hub repositories
    # curl -s  -X DELETE  -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORGANIZATIONNAME}/${REPOSITORY}/tags/${j}/
  else
    echo "This image ${UNAME}/${REPOSITORY}:${j} is within 50 days time range, keep this image"
  fi
done

echo "Script execution ends"