CLI to list and cleanup your GitLab Container Registry and Container Repositories.
Clone this repository and run:
npm install
./gitlab-container-registry-cleaner --help
Note: I can publish this as a package in npm
public registry if someone deems it useful, just ask for it :)
Requirements:
- Node 18+
- GitLab token with
api
scope
Export environment variables to specify your GitLab server and token:
export GITLAB_HOST=https://gitlab.awesomecompany.net
export GITLAB_TOKEN="xxx"
List instance-wide Container Repositories:
# Check all Container Repository from 1 to 10000 with a concurrency of 20 by default
./gitlab-container-registry-cleaner list all
# Customize concurrency and ID range
# Example to check between ID 500-1000 with 10 concurrency (up to 10 requests in parallel)
# Output result to /tmp/repositories.json
./gitlab-container-registry-cleaner list all -s 500 -e 1000 -c 10 -o /tmp/repositories.json
Listing instance wide Container Repositories is done concurrently and may take some time. Internally, it calls GitLab REST API to check every repository ID between 1 and 10000 by default.
You can also list per group or per project:
./gitlab-container-registry-cleaner list project 42
./gitlab-container-registry-cleaner list project mygroup/myproject
./gitlab-container-registry-cleaner list group 666
./gitlab-container-registry-cleaner list group mygroup/mysubgroup
Run cleanup for a project's Container Repository. Note that by default cleanup will dry-run and regex won't match anything for safety reasons.
Example usage:
# Cleanup repository 161
# keep tags matching releases
# Will dry-run by default
./gitlab-container-registry-cleaner clean 161 -k 'v?[0-9]+\.[0-9]+\.[0-9]+.*' -d '.*'
# Output JSON list of tags that would be deleted to a file
# Check their name and created date
./gitlab-container-registry-cleaner clean 161 -k 'v?[0-9]+\.[0-9]+\.[0-9]+.*' -d '.*' --output-tags /tmp/tags.json
cat /tmp/tags.json | jq '.[] | .name + "\t" + .created_at ' -r
# Once satisfied, run without dry-run
./gitlab-container-registry-cleaner clean 161 -k 'v?[0-9]+\.[0-9]+\.[0-9]+.*' -d '.*' --no-dry-run
Repository ID is visible in the URL when navigating to your project Container Repository in Deploy > Container Registry > [repository name]. URL looks like:
# Repository ID is 42
https://gitlab.mycompany.net/somegroup/myproject/container_registry/42
Alternatively, use ./gitlab-container-registry-cleaner list
(see above)
# Keep releases and remove everything else
-k 'v?[0-9]+\.[0-9]+\.[0-9]+.*' -d '.*'
# Delete all tags starting with `dev-`
# '$^' won't match anything
-k '$^' -d '$dev-.*'
# Delete all tags - USE WITH CARE
-k '$^' -d '.*'
Cleanup behavior is similar to GitLab Registry cleanup policy. Cleaner calls GitLab REST API concurrently such as:
- List all tags in repository with List registry repository tags
- Filter out tags not matching keep regex, filter in tags matching delete regex
- Get tag details with Get details of a registry repository tag as List registry repository tags only provide tag name, not creation date
- Filter in tags older than specified number of days
- Delete 'em !