How to delete docker images older than x days from docker hub using a shell script serverfault code example
Example 1: how to delete docker images older than x days from docker hub using a shell script serverfault
#!/bin/bash
set -e
UNAME=$1
UPASS=$2
X=$3
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 "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')
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
for rep in ${REPO_LIST}
do
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
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
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'
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."
else
echo "This image '${UNAME}/${rep}:${tag}' is within $X days time range, keep this image."
fi
done
done
echo
done
echo "Script execution ends here."
Example 2: How to delete docker images from docker hub using a shell script serverfault
#!/bin/bash
set -e
UNAME="YOUR_USERNAME"
UPASS="YOUR_PASSWORD"
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 "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
for i in ${REPO_LIST}
do
IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')
for j in ${IMAGE_TAGS}
do
FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
done
done
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
for i in ${REPO_LIST}
do
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')
for j in ${IMAGE_TAGS}
do
echo
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"
else
echo "This image ${UNAME}/${i}:${j} is within 50 days time range, keep this image"
fi
done
done
echo "Script execution ends"