Purpose

The purpose of this post is to learn how to delete a Docker image tag from Docker Hub. These are some possible scenarios:

  • Using JenkinsCI or other CI/CD tools to create Docker image snapshots and there’s a need to clean old snapshot tags.
  • Clean old tags in order to deprecate old versions.

Delete tag

To delete a tag you need to get and auth token and then perform a delete HTTP call to the registry like the following script:

#!/usr/bin/env bash

USERNAME="docker_username"
PASSWORD="docker_password"
ORGANIZATION="organization"
IMAGE="image"
TAG="tag"

TOKEN=`curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'$USERNAME'", "password": "'$PASSWORD'"}' https://hub.docker.com/v2/users/login/ | jq -r .token`

curl 'https://hub.docker.com/v2/repositories/${ORGANIZATION}/${IMAGE}/tags/${TAG}/' \
-X DELETE \
-H "Authorization: JWT ${TOKEN}"

This script can be used with other Docker image repositories based on Docker registry v2.

More info about Docker Registry HTTP API V2 here: https://docs.docker.com/registry/spec/api/