Delete old GitLab pipelines

Pablo Caballero
BestSecret Tech
Published in
5 min readOct 7, 2022
Delete old pipelines button (fiction).
Delete old pipelines button.

What is the problem?

In GitLab, maybe you miss a “Delete old pipelines” button. Hopefully one day… I trust in the good judgment of GitLab product managers.

The good news is there is a well documented Pipeline API [1] and we are going to use it.

In my opinion, a pipeline fulfils its function during the lifecycle of a branch. Few days later, probably it is useless. You should wonder whether you need those builds, logs, artifacts and triggers or not.

How can I delete them manually?

There is a red button in the top-right corner, in the pipeline page with the caption “Delete”.

Screenshot of delete button.
Screenshot of delete button.

When it is clicked, the developer must confirm the deletion.

Warning message when a user tries to delete a pipeline.
Warning message when a user tries to delete a pipeline.

GitLab will delete all related objects. If you want or need to preserve them, then STOP reading this article.

How can I delete them automatically?

This article describes how to do it step by step. Needless to say, but you must have grants to delete those pipelines.

1 Create a GitLab Access Token for accessing to GitLab API. In GitLab, in top-right corner: Edit Profile> User Settings > Access Tokens

Or directly go to URL: https://gitlab.com/-/profile/personal_access_tokens

Screenshot of Personal Access Token section.
Personal Access Tokens.

The scope should be api and due to security reasons I strongly recommend an expiration date. After clicking on “Create personal access token”, the new code will appear. Please, write it down in a secure place.

New personal access token detail.
New personal access token detail.

2 Look for the project ID. In the main page of your GitLab repository, it is shown (it is a natural number). Please, write it down.

Project ID detail.
Project ID detail.

3 Install Python 3 (latest version): https://www.python.org/downloads/

Python logo.
Photo by Rubaitul Azad on Unsplash

4 Install the following Python libraries:

  • request library [2] is used for using an API:

pip install request

  • python-dateutil library [3] is used to parse ISO 8601 dates:

pip install python-dateutil

5 Download the script delete_pipelines.py. In the next section the different parts will be explained.

Disclaimer of Implied Warranties

EXCEPT AS EXPRESSLY PROVIDED IN THIS AGREEMENT, NEITHER PARTY MAKES ANY OTHER REPRESENTATION OR WARRANTY, EXPRESS OR IMPLIED, EITHER IN FACT OR BY OPERATION OF LAW, STATUTE, OR OTHERWISE, AND EACH PARTY SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED OR STATUTORY WARRANTIES INCLUDING WARRANTIES OF MERCHANTABILITY AND OF FITNESS FOR A PARTICULAR PURPOSE.

Read disclaimer of implied warranties.
Photo by Tingey Injury Law Firm on Unsplash

6In the script delete_pipelines.py, modify the value of the variable ACCESS_TOKEN to your GitLab access token. All the request to GitLab API must be authenticated.

Constant with an access token.

7 In the script delete_pipelines.py, modify the value of the variable PROJECT_ID to your GitLab project ID.

Constant with a project ID.

8 (OPTIONAL) In the script delete_pipelines.py, modify the value of the variable NO_DAYS (number of days) if you want to delete pipelines older than 1 year. It is in days.

Constant with the threshold of days

Explanation:

  • If NO_DAYSis 365(default), all the pipelines younger than 1 year won’t be deleted.
  • If NO_DAYSis 30 , all the pipelines younger than 30 days won’t be deleted.

9Don’t forget saving the script delete_pipelines.py with your changes.

Save your document!
Photo by Vincent Botta on Unsplash

10Finally, run the script delete_pipelines.py.

python delete_pipelines.py

The code

Here there is a brief explanation of the code. So, you can understand it and you could customize delete_pipelines.py for your needs.

Photo by Chris Ried on Unsplash

Constant section

Constant section of delete_pipelines.py script.

The global constants for the step 6, 7 and 8 are at the beginning of the script.

Do NOT upload this code in a repository with an active access token.

I know global variables are the devil. However, it is a really small script.

Method get_old_pipelines

Based on an UTC date, it downloads all the pipeline information. The maximum number of items is 100 so it is calling using the provided pagination mechanism [4].

Method get_old_pipelines of delete_pipelines.py script.

The pipelines are filtered using the field updated_at with a date in ISO 8601 format. E.g. 2016–08–11T11:32:35.169Z

By the way, I need a do-while loop in Python!!! 🐼

Method delete_pipeline

Deletion of a pipeline [1] based on the pipeline ID is requested.

Method delte_pipelines of delete_pipelines.py script.

Method main

It performs the logic at high level. Gets all old pipelines from GitLab and deletes them one by one.

Method main of delete_pipelines.py script.

Full code

Please, have a look at the code.

Viewing some Python codes you could say “it can be done in less lines” (and in a darker way)… Sincerely I prefer a clean code 🐼.

Conclusion

In this article the procedure of how to delete old pipelines in GitLab, so you can safe space and clean up your repositories.

There are a lot of functionalities that can be created with GitLab pipeline API, sky’s the limit!

We have seen how to easily Python can help us with these kind of maintenance scripts.

Than you for your attention

Photo by Wilhelm Gunkel on Unsplash

Please don’t hesitate to contact me if you want to talk about this or anything else.

How to cite this story

Caballero, P. (2022, October). Delete old GitLab pipelines. Medium. Retrieved from https://medium.com/p/7dc61a76e91

References

[1] GitLab. API Docs Pipelines API. Retrieved from https://docs.gitlab.com/ee/api/pipelines.html

[2] Python request library. Requests: HTTP for Humans™. Retrieved from https://requests.readthedocs.io/

[3] Python python-dateutil library. dateutil — powerful extensions to datetime. Retrieved from https://dateutil.readthedocs.io/en/stable/

[4] GitLab. API Docs Pagination. Retrieved from https://docs.gitlab.com/ee/api/index.html#pagination

This story (it isn’t a paper) provided by Pablo Caballero on Medium is for general information purposes only. All information has been provided in good faith, however I make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information on this story.

--

--