Skip to content

Instantly share code, notes, and snippets.

@dannyvankooten
Last active December 9, 2022 18:58
Show Gist options
  • Save dannyvankooten/4566ba895a740a3947658f904fb30851 to your computer and use it in GitHub Desktop.
Save dannyvankooten/4566ba895a740a3947658f904fb30851 to your computer and use it in GitHub Desktop.
Delete all of your GitHub gists w/ Python

Delete all of your GitHub gists

  1. Ensure Python3.x and the requests library is installed.
  2. Create a personal access token and give it the gists permission.
  3. Store the access token in the GITHUB_TOKEN environment value
  4. Run the script
python3 delete-gists.py

Alternatively you can combine step 2 and 3 in a single line:

GITHUB_TOKEN=<your-access-token-here> python3 delete-gists.py

This will delete all of your gists one by one, fetching 200 at a time. If you have more than 200 gists then you will need to run the script more than once to delete all of them.

import requests
import os
token = os.environ.get('GITHUB_TOKEN', '')
if token == '':
print("Please set the GITHUB_TOKEN environment variable before running")
exit()
headers = {
'Accepts': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28'
}
print('Retrieving gists...')
gists = requests.get('https://api.github.com/gists?page=1&per_page=200', headers=headers).json()
choice = input('{} gists found... Do you want to delete them all? Y/[n]: '.format(len(gists)))
if choice != 'Y':
quit()
print('Deleting gists...')
i = 0
for g in gists:
i = i + 1
print('Deleting gist {} ID: {} ...'.format(i, g['id']) )
response = requests.delete('https://api.github.com/gists/{}'.format(g['id']),headers=headers)
if response.status_code == 204:
print('OK')
else:
print('Error...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment