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