Clean up GitLab CI Build artifacts
We've been using a self-hosted GitLab instance for our customer projects since the end of 2015. We immediately jumped on GitLab CI when it was first released to replace our old Jenkins infrastructure with a more modern pipeline-as-code approach.
Over the years, we ran plenty of CI builds and accumulated a few hundred GB of disk space for the build artifacts. A while ago, GitLab introduced a setting for how long build artifacts can be kept around, but sadly, this does not clean up "old" artifacts from before.
To reclaim some disk space, I checked the GitLab docs to see how to "easily" clean up the mess. While navigating the GitLab docs, I came across the Delete project artifacts API endpoint. It sounds exactly what I want.
It can be invoked like this. All you need is an access token and the ID of the project you want to delete the build artifacts for:
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project id>/artifacts"
I've tried this on multiple projects and, as the docs point out, waited as the cleanup will happen during the regular cleanup of expired job artifacts. But nothing ever got cleaned up.
A few days ago, I tried again to clean up old build artifacts. I accidentally came across the glab CLI tool provided by GitLab.
I git cloned all the repositories I wanted to clean up and ran the following command to clean up all artifacts older than 90 days:
glab ci delete --older-than 21960h --paginate
The command will iterate through all CI jobs and delete them individually. If you have plenty of jobs, this will take a while, but in the end, it worked perfectly fine.