Delete All Workflow Runs #2
This file contains hidden or 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
| name: Delete All Workflow Runs | |
| on: | |
| workflow_dispatch: | |
| jobs: | |
| delete-runs: | |
| runs-on: ubuntu-latest | |
| name: Delete All Workflow Runs | |
| permissions: | |
| actions: write | |
| steps: | |
| - name: Delete all workflow runs | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| REPO: ${{ github.repository }} | |
| CURRENT_RUN_ID: ${{ github.run_id }} | |
| run: | | |
| echo "Fetching all completed workflow runs for $REPO..." | |
| deleted=0 | |
| failed=0 | |
| while true; do | |
| response=$(gh api \ | |
| --method GET \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "/repos/$REPO/actions/runs?per_page=100&page=1&status=completed") | |
| count=$(echo "$response" | jq '.workflow_runs | length') | |
| [ "$count" -eq 0 ] && break | |
| while IFS= read -r run_id; do | |
| if [ "$run_id" = "$CURRENT_RUN_ID" ]; then | |
| echo "Skipping current run $run_id" | |
| continue | |
| fi | |
| if gh api \ | |
| --method DELETE \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "/repos/$REPO/actions/runs/$run_id" > /dev/null 2>&1; then | |
| deleted=$((deleted + 1)) | |
| else | |
| echo "Warning: failed to delete run $run_id" | |
| failed=$((failed + 1)) | |
| fi | |
| done < <(echo "$response" | jq -r '.workflow_runs[].id') | |
| done | |
| echo "Deleted $deleted workflow run(s). Failed: $failed." |