Harsh Patel
Harsh Patel

Reputation: 6830

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?

Upvotes: 7

Views: 9822

Answers (4)

zakaria chahboun
zakaria chahboun

Reputation: 511

First of all you need to create a personal access token:

Go to profile/preferences/access tokens or just click here alt text

Repalce your token in token variable in this code bellow:

const axios = require("axios");

// Your authorization token here
const token = "YOUR_ACCESS_TOKEN";

// fetch all projects
axios
  .get("https://gitlab.com/api/v4/projects?visibility=private", {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  })
  .then(async function (response) {
    // get all projects IDs
    let ids = response.data.map((e) => e.id);
    // delete all
    for (let el of ids) {
      await axios.delete(`https://gitlab.com/api/v4/projects/${el}/`, {
        headers: {
          Authorization: `Bearer ${token}`,
        },
      });
    }
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  });

You can change the visibility ?visibility=private or ?visibility=public

Upvotes: 1

Randhir Thakur
Randhir Thakur

Reputation: 15

  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" (i.e. project settings that may result in data loss) or "Remove project" (in newer GitLab versions)
  4. Push this button and follow the instructions

Upvotes: -1

Richard
Richard

Reputation: 769

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.

Upvotes: 2

phd
phd

Reputation: 94943

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.

Upvotes: 10

Related Questions