Delete old GitLab pipelines
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”.
When it is clicked, the developer must confirm the deletion.
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
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.
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.
3 Install Python 3 (latest version): https://www.python.org/downloads/
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.
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.
7 In the script delete_pipelines.py, modify the value of the variable PROJECT_ID
to your GitLab 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.
Explanation:
- If
NO_DAYS
is365
(default), all the pipelines younger than 1 year won’t be deleted. - If
NO_DAYS
is30
, all the pipelines younger than 30 days won’t be deleted.
9Don’t forget saving the script delete_pipelines.py with your changes.
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.
Constant section
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].
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 main
It performs the logic at high level. Gets all old pipelines from GitLab and deletes them one by one.
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
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.