content/blog/2021-07-15-delete-gitlab-repos.org
115 lines · 3558 bytes
1#+date: <2021-07-15 Thu 00:00:00>
2#+title: Method to Remove All GitLab Repositories Through Automation
3#+description: Stepwise instructions for executing a Python script to delete all repositories associated with a GitLab account. Includes authentication and error handling procedures.
4#+slug: delete-gitlab-repos
5#+filetags: :gitlab:python:script:
6
7* Background
8
9Have you ever used GitLab to host your source code, moved to a different
10host, and wanted to delete everything from your GitLab account? Well,
11this post covers any scenario where you would want to delete all
12repositories from your GitLab account.
13
14For me, I currently maintain around 30 repositories and don't like to
15manually delete them whenever I switch host. GitHub has a few different
16tools online to delete all repositories for you, but I have not found
17anything similar for GitLab, so I needed an alternative solution.
18
19* Use a Python Script
20
21** Requirements
22
23Before we look at the script, make sure you know your GitLab username.
24Next, [[https://gitlab.com/-/profile/personal_access_tokens][create an
25authorization token]] so that the Python script can delete your
26repositories. Don't lose this token or else you'll need to create a new
27one.
28
29** Create the Script
30
31To run a Python script, you must first create it. Open a terminal and
32enter the following commands in whichever directory you prefer to store
33the script. You can do the same things in a file manager if you prefer.
34
35#+begin_src sh
36mkdir delete-gitlab
37#+end_src
38
39#+begin_src sh
40cd delete-gitlab
41#+end_src
42
43#+begin_src sh
44nano main.py
45#+end_src
46
47Enter the following code into your =main.py= script.
48
49#+begin_src python
50import request
51import json
52
53
54def get_project_ids():
55 url = "https://gitlab.com/api/v4/users/{user-id}/projects"
56
57 querystring = {"owned": "true", "simple": "true", "per_page": "50"}
58
59 payload = ""
60 headers = {'authorization': 'Bearer {auth-token}'}
61
62 response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
63
64 projects = json.loads(response.text)
65 projects_ids = list(map(lambda project: project.get('id'), projects))
66
67 return projects_ids
68
69
70def remove_project(project_id):
71 url_temp = "https://gitlab.com/api/v4/projects/{project}"
72 headers = {'authorization': 'Bearer {auth-token}'}
73 querystring = ""
74 payload = ""
75
76 url = url_temp.format(project=project_id)
77
78 response = requests.request("DELETE", url, data=payload, headers=headers, params=querystring)
79 project = json.loads(response.text)
80 print(project)
81
82
83def main():
84 projects_ids = get_project_ids()
85
86 url_temp = "https://gitlab.com/api/v4/projects/{project}"
87 headers = {'authorization': 'Bearer {auth-token}'}
88 querystring = ""
89 payload = ""
90
91 for project_id in projects_ids:
92 url = url_temp.format(project=project_id)
93
94 response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
95 project = json.loads(response.text)
96 print(str(project.get('id')) + " " + project.get('name'))
97 print("Removing...")
98 remove_project(project_id)
99
100
101if __name__ == "__main__":
102 main()
103#+end_src
104
105Now that you have the proper information, replace ={user-id}= with your
106GitLab username and ={auth-token}= with the authorization token you
107created earlier.
108
109Finally, simply run the script and watch the output. You can also use
110PyCharm Community Edition to edit and run the Python script if you don't
111want to work in a terminal.
112
113#+begin_src sh
114python3 main.py
115#+end_src