简体   繁体   中英

How to remove all Gitlab repository?

I have created several repositories in GitLab.Now I want to delete or remove all repository at once. How can I do this? is there any API available?

First you list all projects , get a list of IDs and loop over the list: for every project id you remove the project .

You can use a GitLab client (an API wrapper ), they exist for almost any language.

I used the Gitlab's API to list and remove a bulk of projects I migrated by mistake, I made an small python script for it.

DISCLAIMER: BE VERY CAREFUL USING THE FOLLOWING CODE . Read the code thoroughly. you alone are solely and personally responsible for your results.

That being said, here it is:

import requests
import json

def get_project_ids():

    url = "https://gitlab.example.com/api/v4/users/{yourUserId}/projects"

    querystring = {"owned":"true","simple":"true","per_page":"50"}

    payload = ""
    headers = {'authorization': 'Bearer {yourToken}'}

    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.example.com/api/v4/projects/{project}"
    headers = {'authorization': 'Bearer {yourToken}'}
    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.example.com/api/v4/projects/{project}"
    headers = {'authorization': 'Bearer {yourToken}'}
    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()

Replace {yourUserId} and {yourToken} with the corresponding info . Uncomment the remove_project() function to remove projects, again I will not be held responsible or liable in any way for the code presented above.

  1. Go to the project page
  2. Select "Settings"
  3. If you have enough rights at the bottom of the page then will be a button for "Dangerous settings" (ie project settings that may result in data loss) or "Remove project" (in newer GitLab versions)
  4. Push this button and follow the instructions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM