File size: 1,934 Bytes
51ff9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# Script will delete all repositories and tags in your Docker Hub account
set -e

# Set username and password from command-line arguments
UNAME=$1
UPASS=$2

# Get token to interact with 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)

# Ensure token retrieval was successful
if [[ -z "$TOKEN" ]]; then
    echo "Failed to obtain authentication token. Please check your credentials."
    exit 1
fi

# Get list of repositories for that user account
echo "Listing repositories in Docker Hub account '${UNAME}':"
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000" | jq -r '.results|.[]|.name')
if [[ -z "$REPO_LIST" ]]; then
    echo "No repositories found for user '${UNAME}' or failed to fetch repositories."
    exit 1
fi

# Loop through each repository and delete its tags and the repository itself
for rep in ${REPO_LIST}; do
    echo "Processing repository: ${UNAME}/${rep}"

    # Get all tags for the repository
    IMAGES=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${rep}/tags/?page_size=100")
    IMAGE_TAGS=$(echo $IMAGES | jq -r '.results|.[]|.name')

    # Delete each tag
    for tag in ${IMAGE_TAGS}; do
        echo "Deleting tag: ${UNAME}/${rep}:${tag}"
        curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${rep}/tags/${tag}/"
    done

    # Delete the repository itself
    echo "Deleting repository: ${UNAME}/${rep}"
    curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${rep}/" || {
        echo "Failed to delete repository '${UNAME}/${rep}'. Please check permissions or API limits."
    }
    sleep 1
done

echo "Script execution completed."