How to delete a pipeline in Gitlab

As of this writing (v12.1), GitLab still don’t have way to delete pipelines in its web UI, and you can’t control how many pipelines to keep, so yo may end up with a bunch of (failed) pipelines you don’t realy need.

But it seems that in version 11.6 they added an option in the API to delete pipelines, so here is my crud shell script to delete all pipelines in a project and only keep the last X pipelines.


# the project is namespaced so the uri was mynamespace/api-docs
# and the / was uri encoded to %2F

curl \
    -s --header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" \
    ${SERVER}/projects/${PROJECT}/pipelines | jq -r '.[] | .id ' | tail -n+$((KEEP+1)) \
    | xargs -I{} -- curl -s --header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" --request "DELETE" ${SERVER}/projects/${PROJECT}/pipelines/{}

In the first curl call we list all the pipelines in the project, and we should get an json array like this

    "id": 179,
    "sha": "95f1409e45aba9c761629c2777face088f766fdc",
    "ref": "master",
    "status": "success",
    "web_url": "https://git/mynamespace/api-docs/pipelines/179"
    "id": 178,
    "sha": "95f1409e45aba9c761629c2777face088f766fxx",
    "ref": "master",
    "status": "success",
    "web_url": "https://git/mynamespace/api-docs/pipelines/178"

and we use the jq tool to query the result and display on the pipeline id, and then pipe the list to tail which remove the top X id’s we like to keep, and then we call the DELETE API for each of the id’s we got.




You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *