如何批量删除GitLab中的所有仓库?是否有可用API?
Absolutely, you can use GitLab's official REST API to batch delete all your repositories safely. Let me walk you through the process step by step—just make sure you double-check everything before running any deletion commands, since this action can't be undone!
Prerequisites
- A GitLab account with ownership access to the repositories you want to delete.
- A Personal Access Token (PAT) with the right permissions (we'll create this next).
- The
jqtool installed (to parse JSON responses; skip this if you prefer manual parsing, but it makes things way easier).
Step 1: Create a Personal Access Token (PAT)
- Log into your GitLab account, click your avatar in the top-right corner, and select Settings.
- From the left sidebar, go to Access Tokens.
- Enter a token name (e.g., "Batch Delete Repos"), set an expiration date (for security), and check the following scopes:
api(required for API access)delete_repository(required to delete repositories)
- Click Create personal access token, then copy the generated token immediately—you won't see it again!
Step 2: Fetch all your repository IDs
First, let's get a list of all the repository IDs you own. Run this curl command (replace YOUR_PAT with your token, and adjust the GitLab URL if you're using a self-hosted instance):
curl --header "PRIVATE-TOKEN: YOUR_PAT" "https://gitlab.com/api/v4/projects?owned=true&per_page=100" | jq '.[] | .id'
owned=trueensures we only pull repositories you own (skip this if you want to delete repos you're a member of, but adjust the scope accordingly).per_page=100fetches up to 100 repos at once. If you have more than 100, add&page=2(and increment the page number) to get the rest, or use a script to handle pagination automatically.- Add
&include_subgroups=trueif you want to include repositories in subgroups you own.
Step 3: Batch delete repositories with a script
Create a simple shell script to loop through the repository IDs and delete each one. Save this as delete_repos.sh:
#!/bin/bash # Replace these values with your own PAT="YOUR_PERSONAL_ACCESS_TOKEN" GITLAB_URL="https://gitlab.com" # Fetch all owned project IDs (add &include_subgroups=true to include subgroup repos) project_ids=$(curl --silent --header "PRIVATE-TOKEN: $PAT" "$GITLAB_URL/api/v4/projects?owned=true&per_page=100" | jq -r '.[] | .id') # Loop through each ID and delete the project for id in $project_ids; do echo "Attempting to delete project ID: $id" curl --request DELETE --header "PRIVATE-TOKEN: $PAT" "$GITLAB_URL/api/v4/projects/$id" echo -e "\n" done
Then make the script executable and run it:
chmod +x delete_repos.sh ./delete_repos.sh
Critical Notes & Warnings
- Backup first!: Deleting a repository is permanent—GitLab doesn't have a trash recovery feature for deleted repos. Export any important data before proceeding.
- Test with one repo first: To avoid accidental mass deletion, test the delete command on a single repository first:
curl --request DELETE --header "PRIVATE-TOKEN: YOUR_PAT" "https://gitlab.com/api/v4/projects/YOUR_TEST_REPO_ID" - Self-hosted GitLab: If you're using a self-hosted instance, replace
https://gitlab.comwith your instance's URL (e.g.,https://gitlab.yourcompany.com). - Pagination handling: If you have more than 100 repos, modify the script to loop through pages until no more results are returned. For example:
page=1 while true; do project_ids=$(curl --silent --header "PRIVATE-TOKEN: $PAT" "$GITLAB_URL/api/v4/projects?owned=true&per_page=100&page=$page" | jq -r '.[] | .id') if [ -z "$project_ids" ]; then break fi for id in $project_ids; do echo "Deleting project ID: $id" curl --request DELETE --header "PRIVATE-TOKEN: $PAT" "$GITLAB_URL/api/v4/projects/$id" echo -e "\n" done page=$((page+1)) done
内容的提问来源于stack exchange,提问作者Harsh Patel




