Delete All Unused GitHub Repositories Using GitHub API!

Jalal Kiani
Analytics Vidhya
Published in
3 min readOct 12, 2019

I have about 100 forked GitHub repositories (as shown in the following figure), which make my GitHub account very messy. GitHub only allows a user to delete one repository at a time, and it’s not a very user-friendly process. Each time, it asks for your password and also the repository name for every fork. Hence, deleting hundreds of repositories could take a few hours.

My GitHub profile before cleaning

The capability of deleting a large number of GitHub repositories does not exist in the GitHub UI. However, GitHub has a web service API that can be used to delete a bunch of repositories. In this post, I try to demonstrate how we can use the web service API of GitHub and git to delete multiple GitHub repositories.

  1. We assume that you have curl and jq installed. Otherwise, you can use brew install jq on Mac or chocolatey install jq on Windows to install jq.

2. Use the following command line to get the names of all your repositories on your Github account.

curl "https://api.github.com/users/your_GitHub_account/repos?per_page=100&page=1" | jq -r '.[] | .name'
Getting the list of all repositories

This just returns the repositories on page 1 and if you have more than one page you should change page number to also get the list of repositories on other pages.

3. Copy the names of all repositories and paste them in a text editor such as Sublime. Then, just keep the name of all those repositories that you want to delete them. Save the file as “repo_list_deleting.txt” on your Home directory.

The name of all repositories need to be deleted

4. Now you need to add your account name to the first of each line or each repository’s name. Doing this line by line could be very time consuming and boring. In Sublime, you can do it automatically by:

  • Selecting all the lines
  • Going to menu Selection -> Split into Lines (Cmd/Ctrl + Shift + L)

This allows you to edit multiple lines at once. Now you can add “your_account_name/to the beginning of each line.

Adding account name to all repositories name

5. Register a new personal access token with a delete_repo permission at https://github.com/settings/tokens/new

Fill out the Note Section
Check delete_repo and then click on Generate token
Copy your personal access token

6. Finally, use the following command line on Mac:

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

If the above code does not work try to retype the quotations marks in your terminal.

On Windows:

get-content C:\repo_list_deleting.txt | ForEach-Object { Invoke-WebRequest -Uri https://api.github.com/repos/$_ -Method “DELETE” -Headers @{“Authorization”=”token Your_TOKEN”} }

Note that you need to be on your Home directory for running the above command.

--

--