Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daaimah123/7938914de8c40fce9d0a789f2aa7266a to your computer and use it in GitHub Desktop.
Save daaimah123/7938914de8c40fce9d0a789f2aa7266a to your computer and use it in GitHub Desktop.
Organization script for quickly archiving and deleting batches of repos via CLI.
# Function to archive a repository
archive_repo() {
local repo=$1
echo "Archiving repository: $repo"
# Use curl to make PATCH request to GitHub API
if ! curl -X PATCH \
-H "Authorization: token $PAT_ID" \
-H "Accept: application/vnd.github+json" \
-H "Content-Type: application/json" \
-d '{"archived": true}' \
https://api.github.com/repos/$repo; then
echo "Error archiving repository $repo"
return 1
fi
echo "Successfully archived repository: $repo"
}
# Function to delete a repository
delete_repo() {
local repo=$1
echo "Deleting repository: $repo"
# Use curl to make DELETE request to GitHub API
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
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
# Main menu
while true; do
echo ""
echo "1. Archive repositories"
echo "2. Delete repositories"
echo "3. Quit"
read -p "Choose an option: " choice
case $choice in
1)
read -p "Enter repository name to archive (or Ctrl+C to stop): " repo
if [ -n "$repo" ]; then
archive_repo "$repo"
fi
;;
2)
read -p "Enter repository name to delete (or Ctrl+C to stop): " repo
if [ -n "$repo" ]; then
delete_repo "$repo"
fi
;;
3)
echo "Exiting..."
break
;;
*)
echo "Invalid option. Please try again."
;;
esac
done

Manage Organization Repos: Delete & Archive

Deleting and archiving repos via command line interface in the terminal using a script (for organizations); accelerating the time spent on the task if you have a large supply of repos to manage. 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 manage_repos.sh file
  • Replace $PAT_ID with personal access token; or create a separate environment variable file
  • Enable file by running chmod +x manage_repos.sh in the terminal
  • Run the file by running ./manage_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

manage repos script

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