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