How to Delete multiple GitHub repositories at once ?


๐Ÿ‘คย Diwas Poudel ย ย  ๐Ÿ•’ 24 Nov 2022 ย ย  ๐Ÿ“ FIX

You may have built numerous repositories when experimenting with creating projects in Github to learn git and version control. And after a few months or years, you may have decided to delete some or all of the repositories, so you may have tried deleting them one by one by clicking on your desired repository, then going to settings, and finally clicking on Delete this repository, as seen below.

Deleting Single Repo
fig. Deleting Single Repo ๐Ÿ˜…

This is okay if you only have a few repositories. What to do if you have to delete 20-30 repositories. If this is what you are thinking, you have come to the right place. Ourtechroom will help you delete multiple repositories using various methods.

Delete multiple repos from Github

Let's look at two of them.

Method 1: Using Reposweeper Tool

RepoSweeper is a React.js-based utility that allows you to sort, filter, and control which repositories you want to delete at scale with just a single or few clicks of a button.

The steps are as follows:

1. First get the username of the GitHub repository

Log in to your GitHub account. Then, in the top right corner, click the Profile icon. Then, from the list of options, select Your Profile.

Get your Github Username
fig. Get your GitHub Username

Then, in three places, you will discover your Github username ie. in the URL, immediately below your FullName, and in the menu from which you arrived at the profile page.

In my case, GitHub username is: Diwas777

Get Github Username in 3 places
fig. Github Username

2 Goto reposweeper site by clicking here.ย 

3 Scroll down to find the Username and Token Field.

Copy and paste your Username, in my case Diwas777, then click Generate Access Token.

Goto Reposweeper Tool Home Page
fig. Reposweeper Tool

This will redirect you to your Github with the URL: https://github.com/settings/tokens.

4 Click on the "Generate new token" button just next to the Personal access token(classic).

5 From the list click on Generate new token(Classic).

Click on Generate new token button
fig. Generate new Token

6 Give the Note a name, then scroll down and check the โ˜‘ delete repo checkbox, and then click on Generate Token button.

๐Ÿ“š Take note that we have granted remove repoย 
This will generate github token
fig. Generate Token Github

7 Just click on the Windows icon next to the token. This will copy the token to the clipboard.

Copy Token to clipboard
fig. Copy Token to clipboard

8 Now Come back to RepoSweeper and paste down the token in the token field and click on the "Generate Repo List" button.

Paste token to reposweeper and click on Generate Repo List
fig. Paste the token in reposweeper and click on 'Generate repo list'

9 Then wait for some time, you will get all your repository present in your GitHub.

Simply click the topmost checkbox to delete the entire repository. Otherwise, choose all of the checkboxes next to your git repo and then click the Delete button.

Also read:ย HTTPS vs SSH in gitย 

Here I want to delete that 5 repo so I have selected then click on the Delete button as shown below.

Select Repo you want to delete
fig. Select Repos you want to delete

10 Now on the next screen click on the Continue button.

Continue to delete repo
fig. Continue to delete the repo

11 In the next screen, you will get a confirmation like Are you sure you want to delete those selected repos? Just click on the Delete button.

Click on 'Delete' button in confirmation page
fig. Click on the 'Delete' button on the confirmation page

Return to your GitHub and refresh the page. You will not find those deleted repositories.

Method 2: Using the Terminal and command line

First create a list of the repo you want to delete in the form of โ€œyour_username/repo_nameโ€ (not a full path of the repo) per line like shown below and save it into a file let's say: repo_to_delete.txtย 

Linux and macOS

Then if you are in Linux or macOS then run the below command.

while read repo; do curl -X DELETE -H "Authorization: token YOUR_TOKEN_HERE" "https://api.github.com/repos/$repo"; done < repo_to_delete.txt

Windows and Powershell

If you are in Windows then open PowerShell and run the below command and click Enter.

get-content E:\repo_to_delete.txt | ForEach-Object { Invoke-WebRequest -Uri https://api.github.com/repos/$_ -Method "DELETE" -Headers @{"Authorization"="token YOUR_TOKEN_HERE"} }

In windows, I have placed those repo inside the repo_to_detele.txt file inside E drive.

Using Bashย 

Here, we list all of the repos that we want to delete within a variable called repo to delete, and we loop through each of these data, authorizing each of the repos with the token, and then deleting them. Curl is a command-line program that allows us to send requests from clients to servers.

Note that you have to pass your GitHub token in the placeholder YOUR_TOKEN_HERE.

repo_to_delete=(
ย  ย  "username/repo1"
ย  ย  "username/repo2"
ย  ย  "username/repo3"
)

for item in "${repo_to_delete[@]}"
do
ย  ย :ย 
ย  ย curl -XDELETE -H 'Authorization: token YOUR_TOKEN_HERE' "https://api.github.com/repos/$item ";
done