Skip to content

Instantly share code, notes, and snippets.

@HaythmKenway
Last active May 11, 2023 12:38
Show Gist options
  • Save HaythmKenway/b3319f5e0c37dbc00904720e207c5bd8 to your computer and use it in GitHub Desktop.
Save HaythmKenway/b3319f5e0c37dbc00904720e207c5bd8 to your computer and use it in GitHub Desktop.
A bit of automation in removing and changing privacy of Github Repos

Hey today i was going through my github profile and noticed i have created hell a lot of useless github repos simply spamming in my profile

So today i have decided to eradicate all those useless stuffs using gh cli and bash

yeah i could have simply used curl command to do so but, i have never used ghcli in action so wanted to give it a try

for simplicity let us declare username as variable

username=<YOUR-GITHUB-USERNAME>

step 1:

obtain a list of all of your repos

gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /users/$username/repos

This gave me a list of all the public repos in my github account

I have used jq which is a command line json parser to get only the names of the repositorries

gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /users/$username/repos | jq -r '.[] | .name' to get the names of the repos recursively we have used jq

step 2:

Now we can store it to a text file named delete.txt using output redirection operator >

gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /users/$username/repos | jq -r '.[] | .name' >delete.txt

Use your favorite text editor and remove all the repo name which u do not want to delete

Now we can iterate through the content using while in recursive and delete everything using gh api

while read -r repo_name ; do;gh api --method DELETE  -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"  /repos/${username}/${repo_name} ;done <delete.txt; 

This will delete all your useless repositories in github

Same method can be used to update privacy settings if you dont want others to peep into your repositories

Use the following command to do so

while read -r repo_name ; do; gh api --method PATCH  -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"  /repos/${username}/${repo_name} -F private=true\ ;done <private.txt; 

for further references visit github api docs [https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28]

Give a ⭐ to pay respect for the repo containing my portfolio page which i deleted by mistake 🙃

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