blog

GitLab Container Registry Cleanup Script

This script deletes all container registry repositories for a given GitLab project via the API. It fetches repository IDs, then issues a DELETE request for each one.

export GITLAB_ACCESS_TOKEN=xxxxxxxxxxxxxxx
export _PROJECT_ID=32323232323
export GITLAB_URL='gitlab.com'

curl \
  -H "PRIVATE-TOKEN: ${GITLAB_ACCESS_TOKEN}" \
  "https://$GITLAB_URL/api/v4/projects/$_PROJECT_ID/registry/repositories/?per_page=100" | \
    jq '.[] | .id' | \
      xargs \
        -I'{}' \
        curl \
          --fail-with-body \
          --request DELETE \
          --header "PRIVATE-TOKEN: ${GITLAB_ACCESS_TOKEN}" \
          "https://$GITLAB_URL/api/v4/projects/$_PROJECT_ID/registry/repositories/{}"

← all posts