Skip to content

Instantly share code, notes, and snippets.

@vietanhduong
Created October 8, 2021 04:05
Show Gist options
  • Save vietanhduong/0a4d67c739221c6e895d30e7929c9ca3 to your computer and use it in GitHub Desktop.
Save vietanhduong/0a4d67c739221c6e895d30e7929c9ca3 to your computer and use it in GitHub Desktop.
Quick remove github release
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import os
import sys
import json
def err(msg, **kwargs):
print(msg, file=sys.stderr, **kwargs)
def required(value: str, key: str):
if value is None or not len(value):
err(f'${key} is required')
exit(1)
token=os.getenv('GH_TOKEN')
repo = os.getenv('GH_REPO')
owner = os.getenv('GH_OWNER')
# By default, remove all draft release
# If you want to remove all release
# export REMOVE_ALL=1
remove_all = os.getenv('REMOVE_ALL')
required(token, 'GH_TOKEN')
required(repo, 'GH_REPO')
required(owner, 'GH_OWNER')
GH_API='https://api.github.com'
RELEASE_ENDPOINT=f'{GH_API}/repos/{owner}/{repo}/releases'
r = requests.get(RELEASE_ENDPOINT, headers={"Authorization": f'token {token}'})
if r.status_code >= 400:
err(f'Request failed with code: {r.status_code}')
exit(1)
resp = r.json()
for item in resp:
if remove_all is None and not item.get('draft'):
continue
release_id = item.get('id')
r = requests.delete(f'{RELEASE_ENDPOINT}/{release_id}', headers={'Authorization': f'token {token}'})
if r.status_code >= 400:
err(f'Request failed with code: {r.status_code}')
exit(1)
else:
print(f'Release {release_id} was removed')
print('Done !!')
exit(0)
@vietanhduong
Copy link
Author

export GH_TOKEN=ghp_abc
export GH_OWNER=vietanhduong
export GH_REPO=test_repo
# export REMOVE_ALL=1
chmod +x remove_release.py 
./remove_release.py

Be careful with REMOVE_ALL :)

@vietanhduong
Copy link
Author

Create your PAT (Personal Access Token) at https://github.com/settings/tokens

@vietanhduong
Copy link
Author

Remove remote tags

git tag -l | xargs -n 1 git push --delete origin

Remove local tags

git tag -l | xargs -n 1 git tag -d 

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