How can I clean up the latest tags in the registry?

Problem to solve

How can I delete the latest tags in the GitLab Registry using the cleanup policy settings?
Currently, we build the Docker images like this:
docker build . -t $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA -t $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:latest –push
This results in a lot of latest tags remaining from branches that have not existed for years.

Steps to reproduce

Build images with this command:
docker build . -t $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA -t $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:latest –push

Configuration

Provide screenshots from the GitLab UI showing relevant configuration, if applicable.
On self-managed instances, add the relevant configuration settings or changes.


image

Versions

Please select whether options apply, and add the version information.

Versions

The latest tag is always kept using cleanup policies. One workflow change could be to treat Git branches / Git commit refs as tag names, i.e. 1234dfge-feature-xzy-20240716 and not use the commit ref as image name.

Deleting container image tags in bulk can be done through an API action, for example described in Automate storage management | GitLab

2 Likes