Remove all issues from Sentry project (need Python 3.7+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
API_KEY="longrandomstring" # Sentry API key with event:admin scope | |
ORG_SLUG = "organization" # organization slug | |
PROJECT_SLUG = "nice-project" # project slug | |
SENTRY_ROOT = "https://sentry.example.com/" # root Sentry URL | |
BATCH_SIZE = 20 # how many issues deletes by one request (from 1 to 100) | |
headers = {'Authorization': f'Bearer {API_KEY}'} | |
def batch(iterable, n=1): | |
l = len(iterable) | |
for ndx in range(0, l, n): | |
yield iterable[ndx:min(ndx + n, l)] | |
count = 0 | |
while True: | |
response = requests.get( | |
f'{SENTRY_ROOT}/api/0/projects/{ORG_SLUG}/{PROJECT_SLUG}/issues/?query=&statsPeriod=', headers=headers) | |
issues = response.json() | |
count += len(issues) | |
if count % 1000 == 0: | |
print(f'{count} issues was deleted') | |
if len(issues) > 0: | |
for x in batch(issues, BATCH_SIZE): | |
id_query = '' | |
for idx, item in enumerate(x): | |
if idx == 0: | |
id_query += f'?id={item["id"]}' | |
else: | |
id_query += f'&id={item["id"]}' | |
delreq = requests.delete( | |
f'{SENTRY_ROOT}/api/0/projects/{ORG_SLUG}/{PROJECT_SLUG}/issues/{id_query}', headers=headers) | |
if (delreq.status_code != 204): | |
print(f'Some issues are not deleted with status code {delreq.status_code}') | |
else: | |
print('All done! {count} issues was deleted') | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment