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>
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
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 🙃