GitLab Registry Cleanup Policy Not Preserving Version Tags

Hello everyone,

I’ve three tags in my container registry on gitlab :

[
    {
        "name": "1.0.0",
        "path": "myproject/docker/my-image:1.0.0",
        "location": "registry.example.com/myproject/docker/my-image:1.0.0"
    },
    {
        "name": "1.0.0-beta.1",
        "path": "myproject/docker/my-image:1.0.0-beta.1",
        "location": "registry.example.com/myproject/docker/my-image:1.0.0-beta.1"
    },
    {
        "name": "latest",
        "path": "myproject/docker/my-image:latest",
        "location": "registry.example.com/myproject/docker/my-image:latest"
    }
]

I’m trying to delete image tags using the GitLab API, but I want to keep certain tags based on a regex pattern. Here’s the command I’m using:

    curl --request DELETE \
      --data 'name_regex_keep=([0-9]+.[0-9]+.[0-9]+|latest)' \
      --data 'name_regex_delete=.*' \
      --header "PRIVATE-TOKEN: $ACCESS_TOKEN" \
      "$GITLAB_URL/api/v4/projects/$PROJECT_ID/registry/repositories/$REPOSITORY_ID/tags"

According to the regex, it should keep tags that match the pattern x.y.z (e.g., 1.0.0) as well as latest. However, after executing the command, only the latest tag remains.

Could someone help me understand why the 1.0.0 tag is being deleted despite the regex pattern intended to keep it? Any insights or suggestions would be greatly appreciated.

Hmm, looks right to me too.
You could rewrite [0-9]+ to \d.

--data 'name_regex_keep=\d.\d.\d|latest)'

I will try myself later.

1 Like

Hi @botkero,

Thanks for your answer, it worked well with \d.
Here’s my final curl (I added latest-commit also):

curl --request DELETE \
    --data 'name_regex_keep=(\d.\d.\d|latest-commit|latest)' \
    --data 'name_regex_delete=.*' \
    --header "PRIVATE-TOKEN: $ACCESS_TOKEN" \
    "$GITLAB_URL/api/v4/projects/$PROJECT_ID/registry/repositories/$REPOSITORY_ID/tags"
1 Like