19

I create docker image for testing in my Jenkins pipeline, uploading this to Docker hub and deploy those to Kubernetes. At the end of the testing process, I want to delete the test image from Docker hub (not from test machine). How do I delete docker hub image from command line?

6
  • 2
    Possible duplicate of How can I delete Docker's images? Commented May 26, 2017 at 20:26
  • 15
    This is not a duplicate. I want to delete image from Docker hub using command line. Commented May 27, 2017 at 12:08
  • The original version didn't state that. Commented May 30, 2017 at 9:03
  • I have figured out how to do that . This is the final python script I came up with: github.com/appscode/libbuild/blob/master/docker.py#L31 Commented May 30, 2017 at 17:38
  • 4
    Wow, I can't believe there's no docker command line to delete an image from a registry. Commented Nov 16, 2017 at 17:54

6 Answers 6

14

You can delete any <TAG> from your Docker Hub <REPO> by using curl and REST API to the Docker Hub website (at https://hub.docker.com/v2/) rather that to the Docker Hub registry (at docker.io). So if you are not afraid of using an undocumented API, this currently works:

curl -i -X DELETE \
  -H "Accept: application/json" \
  -H "Authorization: JWT $HUB_TOKEN" \
  https://hub.docker.com/v2/repositories/<HUB_USERNAME>/<REPO>/tags/<TAG>/

The HUB_TOKEN is a JSON Web Token passed using Authorization HTTP header, and it can be obtained by posting your credendials in JSON format to the /v2/users/login/ Docker Hub endpoint:

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)

2FA => Personal Access Token

Note than when you have 2FA enabled, you’ll need a personal access token (the only password accepted by the API when using 2FA).

Sign up to request clarification or add additional context in comments.

4 Comments

Getting 403 Forbidden (not 401 Unauthorized) when I do this...
@ThomasHirsch could you try to remove the single quotes in "'${HUB_USERNAME}'" and "'${HUB_PASSWORD}'"? After changing this I was able to remove a tag.
After some testing of getting the HUB_TOKEN (under 2FA), I eliminated all single quotes and switched to the more convoluted but definitely working version with double quotes throughout (escaped when nested)
This works but the API has changed to https://hub.docker.com/v2/namespaces/<org>/repositories/<repo>/tags/<tag>. Reference
13

Use the Docker Hub API as documented in: https://docs.docker.com/v1.7/reference/api/docker-io_api/#delete-a-user-repository

I've just tested a delete of a test image with curl:

curl -X DELETE -u "$user:$pass" https://index.docker.io/v1/repositories/$namespace/$reponame/

Replace $user and $pass with your user and password on the Docker Hub, respectively; and replace $namespace (in my case it's the same as the $user) and $reponame with the image name (in my case was test).

3 Comments

This deletes the full image. How do I delete a particular tag?
There's no way to delete a particular tag with the API at the moment.
The linked doc is dead
5

Dockerhub has a REST backEnd, then you can use it... it is just skipping the FE...

For example:

export USERNAME=myuser
export PASSWORD=mypass
export ORGANIZATION=myorg # (if it's personal, then it's your username)
export REPOSITORY=myrepo
export TAG=latest

curl -u $USERNAME:$PASSWORD -X "DELETE" https://cloud.docker.com/v2/repositories/$ORGANIZATION/$REPOSITORY/tags/$TAG/

This will delete one tag...

In my case, I have microservices, then the REPOSITORY = the Microservice Name...

If I want to delete all the older images, I can iterate on this....

3 Comments

I only get {"detail": "Invalid username/password"} as a response, though I've checked the username and password carefully.
Mr @DavidParks have you check if you are at the right organization? Also, check if you have enough rights... 🤷‍♂️ it could be that you don't have enough rights to the specific repo...
The backend API would have been nice. Looks like the API might have been changed. This solution is not working anymore. At least, I did not get it to work. the curl delete request with user and password is going through without error, but no response from the server
2

You can now use the new BETA (as of 2022-01) Docker Hub API

https://docs.docker.com/docker-hub/api/latest/

and the docker-hub CLI tool among a few other options.

hub-tool login
hub-tool tag rm myrepo/myimage:mytag

3 Comments

is this tested?
@cenestpamoi How do you mean? The CLI tool is built by the folks at Docker, so although officially experimental, it does work in general. github.com/docker/hub-tool#readme
Works perfectly on GitHub actions with an access token: github.com/sudo-bot/docker-rustpython/blob/…
1

It is possible. For a shortcut, Open dev tools in Chrome, go to the network tab. Delete a tag manually from Docker Hub. You will see a request on the network tab in dev tools that goes to https://cloud.docker.com/v2/repositories//tags/. Just right click on that request, Copy, Copy as Curl. It should look something like this...

curl "https://cloud.docker.com/v2/repositories//tags//" -X DELETE -H 'Pragma: no-cache' -H 'Origin: https://cloud.docker.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36' -H 'Accept: application/json' -H 'Cache-Control: no-cache' -H 'Referer: https://cloud.docker.com/user/repository/registry-1.docker.io/reponame/tags' -H 'Cookie: ' --compressed

Comments

1

For any PowerShell friends.

$params = @{username='mickey';password='minnie'}
$response = Invoke-RestMethod -Uri https://hub.docker.com/v2/users/login/ -Method POST -Body $params
$token = $response.token;

$orgName = "mickey" #organization or user name
$repoName = "disney"
$Uri = $("https://hub.docker.com/v2/repositories/$orgName/$repoName/")

Invoke-WebRequest -Method Delete -Uri $Uri -Headers @{Authorization="JWT " + $token; Accept= 'application/json' } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.