Issue #1004
We set expire_in: 1h in our CI test jobs, but old artifacts from days ago were still there. Something wasn’t working right.
After some research, we found this is a known bug in older GitLab versions. If you’re on GitLab 16.7 or older, upgrade to 18.x to fix this. See this forum post and the GitLab docs.
We found the GitLab API has an endpoint to delete all artifacts:
curl --request DELETE \
--header "PRIVATE-TOKEN: <your_access_token>" \
"https://your-gitlab-instance.com/api/v4/projects/<project_id>/artifacts"
This works! But we’d have to find our project ID, create a token, and run this curl command every time. Not ideal.
Automating with a CI Job
We decided to create a CI job that does this automatically. The job uses GitLab’s built-in variables so you don’t have to find your project ID or API URL manually.
Create ci_templates/chore.gitlab-ci.yml:
chore:cleanup-artifacts:
script:
- |
# Delete all artifacts in the project
# Requires GITLAB_API_TOKEN variable with 'api' scope
if [ -z "$GITLAB_API_TOKEN" ]; then
echo "ERROR: GITLAB_API_TOKEN is not set"
echo "Please create a personal access token with 'api' scope and add it as a CI/CD variable"
exit 1
fi
echo "Deleting all artifacts for project ID: $CI_PROJECT_ID"
response=$(curl --request DELETE \
--header "PRIVATE-TOKEN: $GITLAB_API_TOKEN" \
--write-out "\n%{http_code}" \
--silent \
"$CI_API_V4_URL/projects/$CI_PROJECT_ID/artifacts")
http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "202" ]; then
echo "Successfully started artifact deletion (HTTP 202 Accepted)"
else
echo "Failed to delete artifacts. HTTP code: $http_code"
echo "$response"
exit 1
fi
when: manual
allow_failure: true
The job uses GitLab’s built-in variables:
$CI_API_V4_URL- Your GitLab API URL (automatic)$CI_PROJECT_ID- Your project ID (automatic)$GITLAB_API_TOKEN- Your token (you need to add this)
The job is set to when: manual so it won’t run automatically. You click a play button when you want to clean up.
Start the conversation