Skip to content

Instantly share code, notes, and snippets.

@chriskyfung
Created January 3, 2025 01:49
Show Gist options
  • Save chriskyfung/d2f5fbabc330c5fc39b749e25c9b4429 to your computer and use it in GitHub Desktop.
Save chriskyfung/d2f5fbabc330c5fc39b749e25c9b4429 to your computer and use it in GitHub Desktop.
A bash script to delete all workflow runs for GitHub Actions workflows with the status "disabled_manually".
#! /bin/bash
# Script to delete all workflow runs for workflows with the status "disabled_manually"
# This script uses the --jq option of GitHub CLI to filter JSON data directly,
# avoiding the need to pipe through the jq command.
# Modified from: https://stackoverflow.com/a/67000032
# Define your organization and repository
org=<your org>
repo=<your repo>
# Get workflow IDs with status "disabled_manually"
workflow_ids=($(gh api repos/$org/$repo/actions/workflows --paginate --jq '.workflows[] | select(.["state"] | contains("disabled_manually")) | .id'))
# Loop through each workflow ID and delete its runs
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