Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r0mdau/4e8556ef6c8cfe14063ec7d35a7d0144 to your computer and use it in GitHub Desktop.
Save r0mdau/4e8556ef6c8cfe14063ec7d35a7d0144 to your computer and use it in GitHub Desktop.
One liner for deleting images with all associated tags and a progress bar 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.

#!/bin/bash
registry='localhost:5000'
name='my-image'

tags=$(curl -sSL "http://${registry}/v2/${image}/tags/list" | jq -r '.tags[]')
readarray -t count <<<"${tags}"
tagsCount=${#count[@]}

i=0
for tag in $tags; do
  echo -n -e "Deleting ${image}:${tag}"

  curl -sSL -X DELETE "http://${registry}/v2/${image}/manifests/$(
    curl -sSL -I \
        -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
        "http://${registry}/v2/${image}/manifests/$tag" \
    | awk '$1 == "Docker-Content-Digest:" { print $2 }' \
    | tr -d $'\r' \
  )"
  ((i=i+1))

  progress=$(($((${i}*100))/${tagsCount}))
  echo " - ${progress}% done"
done
echo "$i deleted images"

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment