Home GitHub: Script to Mass Delete Repos
Post
Cancel

GitHub: Script to Mass Delete Repos

Overview

If you’ve ever had to delete several repositories in GitHub, you know it can be a pain as you have to copy/paste the name of each repo in the verification prompt. This script aims to simplify that by providing a list of repositories that you want to delete in a CSV, and then looping through each repo and deleting it with the gh cli.

The Scripts

These scripts are in my github-misc-scripts repo:

Using the Scripts

Prerequisite: You need to make sure to have the gh cli installed and authorized (gh auth login).

  1. Prepare a list of repositories that you want to delete and place in a CSV file, one per line, with the last line empty.
    • You can use the generate-repos-list.sh script to generate a list of repos in a GitHub org, and then modify accordingly:
      1
      
        ./generate-repos.sh octohol > repos.csv
      
    • Or, create your own CSV file with the list of repos you want to delete, one per line, and leave a trailing empty line/whitespace at the end of the file. The file should look like:
      1
      2
      3
      
        org/repo1
        org/repo2
              
      
  2. From there, it’s pretty simple - run the script, passing in the repos.csv file:
    1
    
     ./delete-repos-from-list.sh repos.csv
    

Summary

If you accidentally delete a repository that you didn’t mean to, the good news is that you have 90 days to recover it in most cases (with some caveats for forks).

Hopefully this script saves you some time in the UI copy/pasting repo names when you want to do some spring cleaning!

This post is licensed under CC BY 4.0 by the author.

Migrating Repos to GitHub

Using the GitHub Checks API to Link Workflow Statuses in a PR