Skip to content

Instantly share code, notes, and snippets.

@hguemar
Created June 2, 2016 16:44
Show Gist options
  • Save hguemar/6eec2f7c5a6e9436bba4d495f4b49235 to your computer and use it in GitHub Desktop.
Save hguemar/6eec2f7c5a6e9436bba4d495f4b49235 to your computer and use it in GitHub Desktop.
#! /usr/bin/python3
import argparse
from operator import attrgetter
import os
import sys
from github import Github
def main():
desc = 'List and delete fork repositories in a GitHub org (env: GH_TOKEN)'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--github-token',
help='GitHub API Token',
dest='token',
default=os.environ.get('GH_TOKEN', None),)
parser.add_argument('-d,--delete',
help='delete forks in organization',
dest='delete',
action='store_true')
parser.add_argument('--org',
help='github organization',
dest='org',
required=True)
args = parser.parse_args()
if not args.token:
sys.exit(parser.print_usage())
g = Github(args.token)
forks = retrieve_forks(g, args.org)
if args.delete:
answer = input('please confirm deletion! [y/n]\n')
if answer == 'yes' or answer == 'y':
delete_forks(forks)
else:
list_forks(forks)
def list_forks(forks):
for fork in forks:
print(fork.name)
def delete_forks(forks):
for fork in forks:
fork.delete()
def retrieve_forks(g, org):
org = g.get_organization(org)
repos = org.get_repos('forks')
forks = [repo for repo in repos]
return sorted(forks, key=attrgetter('name'))
if __name__ == '__main__':
main()
@hguemar
Copy link
Author

hguemar commented Jun 2, 2016

./cleanup-forks --github-token XXX --org rdo-packages # list forked repositories
./cleanup-forks --github-token XXX --org rdo-packages -d # delete forked repositories

@pa-0
Copy link

pa-0 commented Jun 3, 2024

Additionally, if you'd like to continue to follow / support those repos that you forked, you can run this one-liner before you delete all the forks:

gh repo list <USERNAME> --fork --limit 9999 --json parent --jq '.[]|(.parent|.owner as $t|"\($t.login)/\(.name)")' | while read -r parent_; do gh api \ --method PUT \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ /user/starred/"$parent"

Just replace <USERNAME> with your GitHub username WITHOUT the < or >. This will star all GitHub repositories forked by a User.


Note

GitHub CLI needs to be installed before you can run this command.

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