Instantly share code, notes, and snippets.

Embed
What would you like to do?
One liner for deleting images from a v2 docker registry

One liner for deleting images from a v2 docker registry

Just plug in your own values for registry and repo/image name.

registry='localhost:5000'
name='my-image'
curl -v -sSL -X DELETE "http://${registry}/v2/${name}/manifests/$(
    curl -sSL -I \
        -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
        "http://${registry}/v2/${name}/manifests/$(
            curl -sSL "http://${registry}/v2/${name}/tags/list" | jq -r '.tags[0]'
        )" \
    | awk '$1 == "Docker-Content-Digest:" { print $2 }' \
    | tr -d $'\r' \
)"

If all goes well

* About to connect() to localhost port 5000 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5000 (#0)
> DELETE /v2/my-image/manifests/sha256:14f6ecba1981e49eb4552d1a29881bc315d5160c6547fdd100948a9e30a90dff HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:5000
> Accept: */*
>
< HTTP/1.1 202 Accepted
< Docker-Distribution-Api-Version: registry/2.0
< X-Content-Type-Options: nosniff
< Date: Wed, 15 Nov 2017 23:25:30 GMT
< Content-Length: 0
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact

Garbage cleanup

Finally, invoke garbage cleanup on the docker-registry container.

For example:

docker exec -it docker-registry bin/registry garbage-collect /etc/docker/registry/config.yml
@IoTPlay

This comment has been minimized.

Show comment
Hide comment
@IoTPlay

IoTPlay Jan 28, 2018

thanks ! I get an error, do you have an idea where I should look?

*   Trying fe80::1ee9:8168:e5f7:5ad...
* TCP_NODELAY set
* Connected to rh01.local (fe80::1ee9:8168:e5f7:5ad) port 5000 (#0)
> DELETE /v2/alpine-mariadb/manifests/sha256:bab2e6d660df03fd1d6f5285f7026755e34c25f32d1c7f5a3e2688bbc038c458 HTTP/1.1
> Host: rh01.local:5000
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 405 Method Not Allowed
< Content-Type: application/json; charset=utf-8
< Docker-Distribution-Api-Version: registry/2.0
< X-Content-Type-Options: nosniff
< Date: Sun, 28 Jan 2018 10:48:21 GMT
< Content-Length: 78
< 
{"errors":[{"code":"UNSUPPORTED","message":"The operation is unsupported."}]}
* Connection #0 to host rh01.local left intact

IoTPlay commented Jan 28, 2018

thanks ! I get an error, do you have an idea where I should look?

*   Trying fe80::1ee9:8168:e5f7:5ad...
* TCP_NODELAY set
* Connected to rh01.local (fe80::1ee9:8168:e5f7:5ad) port 5000 (#0)
> DELETE /v2/alpine-mariadb/manifests/sha256:bab2e6d660df03fd1d6f5285f7026755e34c25f32d1c7f5a3e2688bbc038c458 HTTP/1.1
> Host: rh01.local:5000
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 405 Method Not Allowed
< Content-Type: application/json; charset=utf-8
< Docker-Distribution-Api-Version: registry/2.0
< X-Content-Type-Options: nosniff
< Date: Sun, 28 Jan 2018 10:48:21 GMT
< Content-Length: 78
< 
{"errors":[{"code":"UNSUPPORTED","message":"The operation is unsupported."}]}
* Connection #0 to host rh01.local left intact
@MoLow

This comment has been minimized.

Show comment
Hide comment
@MoLow

MoLow May 30, 2018

You should set the environment variable REGISTRY_STORAGE_DELETE_ENABLED=true

MoLow commented May 30, 2018

You should set the environment variable REGISTRY_STORAGE_DELETE_ENABLED=true

@vanbroup

This comment has been minimized.

Show comment
Hide comment
@vanbroup

vanbroup Jul 24, 2018

A small update for when using https and basic auth:

registry='localhost:5000'
name='my-image'
auth='-u username:password'
curl -$auth -v sSL -X DELETE "https://${registry}/v2/${name}/manifests/$(
    curl -$auth -sSL -I \
        -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
        "https://${registry}/v2/${name}/manifests/$(
            curl -$auth -sSL "https://${registry}/v2/${name}/tags/list" | jq -r '.tags[0]'
        )" \
    | awk '$1 == "Docker-Content-Digest:" { print $2 }' \
    | tr -d $'\r' \
)"

vanbroup commented Jul 24, 2018

A small update for when using https and basic auth:

registry='localhost:5000'
name='my-image'
auth='-u username:password'
curl -$auth -v sSL -X DELETE "https://${registry}/v2/${name}/manifests/$(
    curl -$auth -sSL -I \
        -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
        "https://${registry}/v2/${name}/manifests/$(
            curl -$auth -sSL "https://${registry}/v2/${name}/tags/list" | jq -r '.tags[0]'
        )" \
    | awk '$1 == "Docker-Content-Digest:" { print $2 }' \
    | tr -d $'\r' \
)"
@ykyuen

This comment has been minimized.

Show comment
Hide comment
@ykyuen

ykyuen Jul 30, 2018

thanks for the snippet, i made a small change in the regular expression such that it could get the "Docker-Content-Digest" case-insensitively.

moreover, you could specify the tag which u want to delete. here you go.

registry='<registry host>'
name='<image name>'
auth='-u <username>:<password>'
tag='<tag>'
curl $auth -X DELETE -sI -k "https://${registry}/v2/${name}/manifests/$(
  curl $auth -sI -k \
    -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
    "https://${registry}/v2/${name}/manifests/${tag}" \
    | tr -d '\r' | sed -En 's/^Docker-Content-Digest: (.*)/\1/pi'
)"

ykyuen commented Jul 30, 2018

thanks for the snippet, i made a small change in the regular expression such that it could get the "Docker-Content-Digest" case-insensitively.

moreover, you could specify the tag which u want to delete. here you go.

registry='<registry host>'
name='<image name>'
auth='-u <username>:<password>'
tag='<tag>'
curl $auth -X DELETE -sI -k "https://${registry}/v2/${name}/manifests/$(
  curl $auth -sI -k \
    -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
    "https://${registry}/v2/${name}/manifests/${tag}" \
    | tr -d '\r' | sed -En 's/^Docker-Content-Digest: (.*)/\1/pi'
)"

@petershaw

This comment has been minimized.

Show comment
Hide comment
@petershaw

petershaw Aug 28, 2018

I am on registry 2.6.2 and I have enabled delete in the config.yml.
The delete call give me a 405. Do someone have a hint?

HTTP/1.1 405 Method Not Allowed
Server: nginx/1.13.3
Date: Tue, 28 Aug 2018 17:04:20 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 78
Connection: keep-alive
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff

petershaw commented Aug 28, 2018

I am on registry 2.6.2 and I have enabled delete in the config.yml.
The delete call give me a 405. Do someone have a hint?

HTTP/1.1 405 Method Not Allowed
Server: nginx/1.13.3
Date: Tue, 28 Aug 2018 17:04:20 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 78
Connection: keep-alive
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment