Skip to content

Instantly share code, notes, and snippets.

@gabrielmocanu
Created November 15, 2023 12:19
Show Gist options
  • Save gabrielmocanu/a00ec1c9b10928605b38d39120b6da98 to your computer and use it in GitHub Desktop.
Save gabrielmocanu/a00ec1c9b10928605b38d39120b6da98 to your computer and use it in GitHub Desktop.
Delete old GitHub Actions workflow runs
#!/bin/bash
# This is a script to remove old GitHub Actions workflow runs for a given workflow path
# It is helpful for cleaning up old workflow runs after you renamed the workflow
org=<ORGANIZATION-NAME>
repo=<REPOSITORY-NAME>
workflow_path=".github/workflows/<OLD_WORKFLOW_NAME>"
# Get workflow IDs with path = $path_workflow
workflow_ids=($(gh api repos/$org/$repo/actions/workflows --paginate | jq '.workflows[] | select(.path == '\"$workflow_path\"') | .id'))
echo "Found ${#workflow_ids[@]} workflows with path $workflow_path"
for workflow_id in "${workflow_ids[@]}"; do
echo "Listing runs for the workflow ID $workflow_id"
run_ids=($(gh api repos/$org/$repo/actions/workflows/$workflow_id/runs --paginate --jq '.workflow_runs[].id'))
for run_id in "${run_ids[@]}"; do
echo "Deleting Run ID $run_id"
gh api repos/$org/$repo/actions/runs/$run_id -X DELETE >/dev/null
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment