15

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?

codefx
  • 9,872
  • 16
  • 53
  • 81
  • 2
    Possible duplicate of [How can I delete Docker's images?](https://stackoverflow.com/questions/21398087/how-can-i-delete-dockers-images) – ipinak May 26 '17 at 20:26
  • 13
    This is not a duplicate. I want to delete image from Docker hub using command line. – codefx May 27 '17 at 12:08
  • The original version didn't state that. – ipinak May 30 '17 at 09:03
  • 4
    Wow, I can't believe there's no docker command line to delete an image from a registry. – wisbucky Nov 16 '17 at 17:54
  • I have figured out how to do that . This is the final python script I came up with: https://github.com/appscode/libbuild/blob/master/docker.py#L31 – codefx May 30 '17 at 17:38
  • The question refers to Docker *Hub* and its `hub.docker.io` API does allow for deleting tags, like [my answer here shows](https://stackoverflow.com/questions/44209644/how-do-i-delete-a-docker-image-from-docker-hub-via-command-line/59334315#59334315) – mirekphd Dec 14 '19 at 14:24

6 Answers6

12

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).

Ricardo Branco
  • 5,740
  • 1
  • 21
  • 31
8

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).

mirekphd
  • 4,799
  • 3
  • 38
  • 59
  • 1
    Getting `403 Forbidden` (not `401 Unauthorized`) when I do this... – Thomas Hirsch Jun 19 '20 at 08:55
  • @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. – 030 Jun 01 '21 at 08:19
  • 1
    @030 Thanks, I had solved it in the meantime. – Thomas Hirsch Jun 01 '21 at 11:17
  • 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) – mirekphd Jul 05 '22 at 15:46
  • This works but the API has changed to `https://hub.docker.com/v2/namespaces//repositories//tags/`. [Reference](https://docs.docker.com/docker-hub/api/latest/#tag/repositories) – pat-s Apr 26 '23 at 10:27
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....

William Desportes
  • 1,412
  • 1
  • 22
  • 31
Marco Vargas
  • 1,232
  • 13
  • 31
  • I only get `{"detail": "Invalid username/password"}` as a response, though I've checked the username and password carefully. – David Parks Sep 19 '20 at 02:52
  • 2
    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... – Marco Vargas Sep 21 '20 at 22:50
  • 1
    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 – Alex Berger Nov 11 '22 at 11:26
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' } 
Mike Casas
  • 763
  • 1
  • 8
  • 14
1

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
spkane
  • 6,177
  • 2
  • 18
  • 18
  • is this tested? – cestpasmoi Dec 20 '22 at 13:08
  • @cenestpamoi How do you mean? The CLI tool is built by the folks at Docker, so although officially experimental, it does work in general. https://github.com/docker/hub-tool#readme – spkane Jan 17 '23 at 21:26
  • Works perfectly on GitHub actions with an access token: https://github.com/sudo-bot/docker-rustpython/blob/53f16c3265db556be0248b45281180e319df492c/.github/workflows/publish.yml#L106 – William Desportes Feb 25 '23 at 01:16
0

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