Skip to content

Instantly share code, notes, and snippets.

@daaimah123
Last active January 2, 2025 07:36
Show Gist options
  • Save daaimah123/4b55d399ad197f63465ad9cdcf59eb85 to your computer and use it in GitHub Desktop.
Save daaimah123/4b55d399ad197f63465ad9cdcf59eb85 to your computer and use it in GitHub Desktop.
Org script for deleting multiple repos repos via CLI.
#!/bin/bash
echo "Enter repository names (one per line), or Ctrl+C to stop:"
# Function to delete a repository
delete_multiple_repos() {
local repo=$1
echo "Deleting repository: $repo"
# Use curl to make DELETE request to GitHub API
# Replace $PAT_ID variable with personal access token
if ! curl -X DELETE \
-H "Authorization: token $PAT_ID" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version:2022-11-28" \
https://api.github.com/repos/$repo; then
echo "Error deleting repository $repo"
return 1
fi
echo "Successfully deleted repository: $repo"
}
# Check if PAT_ID environment variable is set
# Replace $PAT_ID variable with personal access token
if [[ -z "$PAT_ID" ]]; then
echo "Error: PAT_ID environment variable is not set."
echo "Please set PAT_ID with your GitHub Personal Access Token."
exit 1
fi
# Read repositories from stdin (one per line)
while IFS= read -r repo; do
delete_multiple_repos "$repo"
done

Multiple Remote Repository Deletion Script

Deleting multiple repos via command line interface in the terminal using a script (for organizations); saving hours of time if you have a large supply of repos to delete. Note if you are doing this for an individual user profile, you will need to adjust the script slightly.

😩 Existing GitHub process for deleting a remote repository is arduous for large batches

delete stale repos

βš™οΈ Setting up the ./manage_repos.sh script

  • Generate a fine-grained Personal Access Token ID ($PAT_ID)
  • Request that the organization accept the token, review permissions
  • Test for org-repo permissions, run in the terminal: curl -H "Authorization: Bearer $PAT_ID" https://api.github.com/repos/<Organization-Name>/<repo-name>
  • Download the delete_multiple_repos.sh file
  • Replace $PAT_ID with personal access token; or create a separate environment variable file
  • Enable file by running chmod +x delete_multiple_repos.sh in the terminal
  • Run the file by running ./delete_multiple_repos.sh in the terminal

πŸ‘©πŸ½β€πŸ’» Grabbing repo names from https://github.com/orgs/<org-name>/repositories

grab repo names

πŸ“ Formatting your repository list for pasting into terminal

create management list

πŸ€– Running & using the script

use delete multiple repos script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment