DEV Community

Picoable
Picoable

Posted on • Originally published at bashlinux.picoable.com

Deleting GitHub Repositories Without Prompt: A Guide to Automated Cleanup

Managing numerous GitHub repositories can sometimes be a chore, especially when you need to clean up a large number of old projects, test repositories, or deprecated forks. Manually deleting each repository through the web interface or even interactively via the GitHub CLI (gh) can be time-consuming and repetitive.

This guide will show you how to automate the process of deleting multiple GitHub repositories using a simple bash script combined with the powerful GitHub CLI, all without being prompted for confirmation for each deletion.

⚠️ WARNING: This script performs destructive actions. Repositories deleted are gone forever. Always double-check your list of repositories before running any deletion script. It is highly recommended to test this process on dummy repositories first.

Prerequisites

Before you begin, ensure you have the following installed and configured:

  1. GitHub CLI (gh): This is the official command-line tool for GitHub. If you don't have it, install it from cli.github.com.
  2. Authenticated gh CLI: Make sure you are logged in to your GitHub account via the CLI. You can do this by running gh auth login.

Step 1: Generate a List of Repositories to Delete

The first step is to get a list of the repositories you intend to delete. We'll use gh repo list to fetch repository names and pipe the output to a file.

The gh repo list command is highly flexible. You can filter by owner, visibility (public, private, internal), and more. For this example, let's assume you want to delete all public repositories owned by your current user.

gh repo list --public --json name --jq '.[].name' -L 9999 > deletrepo.txt
Enter fullscreen mode Exit fullscreen mode

Let's break down this command:

  • gh repo list: Lists repositories.
  • --public: Filters to only show public repositories. You could use --private or --all as needed.
  • --json name: Outputs the repository data in JSON format, specifically extracting only the name field.
  • --jq '.[].name': Uses jq (a lightweight and flexible command-line JSON processor) to extract just the name value from each JSON object in the array. This ensures deletrepo.txt contains one repository name per line.
  • -L 9999: Sets the limit to list up to 9999 repositories. Adjust this if you have more.
  • > deletrepo.txt: Redirects the output to a file named deletrepo.txt.

Crucial Step: Review deletrepo.txt

After running the above command, open deletrepo.txt in a text editor and carefully review its contents. Ensure that only the repositories you genuinely want to delete are listed. Remove any entries you wish to keep. This is your last chance to prevent accidental deletion!

Example deletrepo.txt:

my-old-project
test-repo-1
another-unused-app
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Deletion Script (delete_repos.sh)

Now, we'll create a bash script that reads each repository name from deletrepo.txt and uses gh repo delete to remove it. The key to automating without prompts is the --confirm flag.

Create a file named delete_repos.sh and add the following content:

#!/bin/bash

# This script reads a list of repository names from deletrepo.txt
# and deletes each one using the GitHub CLI.
# USE WITH EXTREME CAUTION! Repositories deleted are gone forever.

REPO_LIST_FILE="deletrepo.txt"

# Check if the repository list file exists
if [ ! -f "$REPO_LIST_FILE" ]; then
  echo "Error: Repository list file '$REPO_LIST_FILE' not found."
  echo "Please create it using 'gh repo list --public --json name --jq '.[].name' -L 9999 > $REPO_LIST_FILE' and review its contents."
  exit 1
fi

echo "Reading repositories from $REPO_LIST_FILE..."
echo "Starting deletion process. This is irreversible!"
echo "-------------------------------------------------"

# Read each line (repository name) from the file
while IFS= read -r repo_name; do
  # Skip empty lines
  if [ -z "$repo_name" ]; then
    continue
  fi

  echo "Attempting to delete repository: $repo_name"
  # Use gh repo delete with --confirm to bypass the interactive prompt
  gh repo delete "$repo_name" --confirm

  if [ $? -eq 0 ]; then
    echo "✅ Successfully deleted $repo_name"
  else
    echo "❌ Failed to delete $repo_name. Check gh CLI output for details."
  fi
  echo "-------------------------------------------------"
done < "$REPO_LIST_FILE"

echo "Deletion process complete."
echo "It is recommended to run 'gh repo list' to verify remaining repositories."
Enter fullscreen mode Exit fullscreen mode

Step 3: Make the Script Executable and Run It

  1. Make the script executable:

    chmod +x delete_repos.sh
    
  2. Run the script:

    ./delete_repos.sh
    

The script will iterate through deletrepo.txt, attempting to delete each repository. You will see output for each deletion attempt, indicating success or failure.

Safety Measures and Best Practices

  • Always Review deletrepo.txt: This cannot be stressed enough. A mistake here can lead to irreversible data loss.
  • Test on Dummy Repositories: Create a few throwaway repositories and test the entire process before using it on real data.
  • Backup Important Data: If there's any chance a repository might contain valuable information, ensure you have a backup before deleting it.
  • Use Specific Filters: When generating deletrepo.txt, be as specific as possible with gh repo list filters (e.g., --owner YOUR_USERNAME, --topic old-projects) to minimize the risk of including unintended repositories.
  • Consider Archiving: If you're unsure about permanent deletion, GitHub allows archiving repositories, which makes them read-only but preserves their history.

Conclusion

Automating GitHub repository deletion can be a huge time-saver for developers and teams managing many projects. By combining the power of the GitHub CLI with a simple bash script, you can efficiently clean up your GitHub presence. Just remember to proceed with extreme caution and verify your target list thoroughly!

Top comments (0)