Skip to content

Instantly share code, notes, and snippets.

@louneskmt
Last active June 22, 2023 17:24
Show Gist options
  • Save louneskmt/816343c573d11753e7842b7be2a3ac2e to your computer and use it in GitHub Desktop.
Save louneskmt/816343c573d11753e7842b7be2a3ac2e to your computer and use it in GitHub Desktop.
Remove all failed workflow runs in a GitHub repository (up to 100)
#!/bin/bash
ORG_NAME=$1
REPO_NAME=$2
if [[ -z REPO_NAME || -z ORG_NAME ]]; then
echo "Usage: bash remove_failed_workflows.sh <USER/ORG NAME> <REPO NAME>"
exit 1
fi
if ! gh auth status 2>&1 >/dev/null | grep "Logged in to github.com"; then
echo "Script requires logged-in gh CLI user."
echo "Install gh with \`brew install gh\`"
exit 1
fi
JQ_SEARCH=".workflow_runs[] | select(.conclusion == \"failure\") | \"\\(.id)\""
echo "Retreiving runs..."
WORKFLOW_RUNS=($(gh api "repos/$ORG_NAME/$REPO_NAME/actions/runs?per_page=100" | jq -r "$JQ_SEARCH"))
echo "Done!"
read -r -p "Are you sure you want to delete all failed runs? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
DELETED=1
LEFT=${#WORKFLOW_RUNS[@]}
for id in "${WORKFLOW_RUNS[@]}"
do
# Be lazy and try to avoid fetching stale results
sleep 1
echo "Deleting $DELETED of $LEFT"
$(gh api repos/$ORG_NAME/$REPO_NAME/actions/runs/$id -X DELETE)
((DELETED=DELETED+1))
done;
echo ""
echo "All Done! Deleted $DELETED failed runs."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment