Skip to content

Instantly share code, notes, and snippets.

@CaueP
Last active September 26, 2023 14:45
Show Gist options
  • Save CaueP/606a309c7b0c27203c81047678f2deea to your computer and use it in GitHub Desktop.
Save CaueP/606a309c7b0c27203c81047678f2deea to your computer and use it in GitHub Desktop.
GitHub Actions - Cleap up disabled workflows
#########################################################
# Instructions:
# 1. Install Github CLI and setup your credentials: https://cli.github.com/
# 2. Change org and repo here
org=YOUR_ORG_NAME
repo=YOUR_REPO_NAME
# 3. Manually disable the workflows you want to clean up on your repo Actions page
# 4. Run the script (you may need to change its execution permissions): bash -x ./cleanup-actions.sh
# Script based on https://stackoverflow.com/questions/57927115/delete-a-workflow-from-github-actions
#########################################################
# 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'))
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 of $workflow_id"
# Sleep to avoid hitting Github request limits
sleep 0.5
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