Skip to content

Instantly share code, notes, and snippets.

@esc5221
Created December 14, 2022 10:36
Show Gist options
  • Save esc5221/467c7fad71cf67a0ac08805e683d9644 to your computer and use it in GitHub Desktop.
Save esc5221/467c7fad71cf67a0ac08805e683d9644 to your computer and use it in GitHub Desktop.
delete github actions workflow runs by name
# resolves : Is there a way to delete or hide old/renamed Workflows? #26256
# https://github.com/community/community/discussions/26256
from multiprocessing.dummy import Pool as ThreadPool
import requests
TOKEN = "{YOUR_GH_TOKEN}"
OWNER_REPO = "esc5221/example_repo"
DELETE_TARGET_RUN_NAME = "{RUN NAME}"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {TOKEN}",
}
PER_PAGE = 100
def list_all_runs(owner_repo):
url = f"https://api.github.com/repos/{owner_repo}/actions/runs"
url_with_params = f"{url}?per_page=1"
total_count = requests.get(url, headers=headers).json()["total_count"]
page_count = total_count // PER_PAGE + 1
print("total_count: ", total_count)
def _list_runs_by_page(page):
url_with_params = f"{url}?page={page}&per_page={PER_PAGE}"
runs_list = requests.get(url_with_params, headers=headers).json()
page_workflow_runs = runs_list["workflow_runs"]
return page_workflow_runs
merged_workflow_runs = []
with ThreadPool(10) as p:
merged_workflow_runs = p.map(_list_runs_by_page, range(1, page_count + 1))
return [run for runs in merged_workflow_runs for run in runs]
def filter_runs_by_name(runs_list, name):
filtered_runs = [run for run in runs_list if run["name"] == name]
return filtered_runs
def remove_runs(owner_repo, runs_list):
def remove_run_by_id(run_id):
url = f"https://api.github.com/repos/{owner_repo}/actions/runs/{run_id}"
requests.delete(url, headers=headers)
with ThreadPool(10) as p:
p.map(remove_run_by_id, [run["id"] for run in runs_list])
print("removed runs: ", len(runs_list))
def main():
runs = list_all_runs(OWNER_REPO)
delete_target_runs = filter_runs_by_name(runs, DELETE_TARGET_RUN_NAME)
remove_runs(OWNER_REPO, delete_target_runs)
if __name__ == "__main__":
main()
@awoimbee
Copy link

Thanks ! ❤️

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