Skip to content

Instantly share code, notes, and snippets.

@pckilgore
Last active May 10, 2023 07:24
Show Gist options
  • Save pckilgore/827bafd1bbda456b30ebd32ff652f9b6 to your computer and use it in GitHub Desktop.
Save pckilgore/827bafd1bbda456b30ebd32ff652f9b6 to your computer and use it in GitHub Desktop.
Batch Delete Github Workflow Runs Shell Script
#!/bin/bash
ORG_NAME=$1
REPO_NAME=$2
WORKFLOW_NAME=$3
if [[ -z $WORKFLOW_NAME || -z REPO_NAME || -z ORG_NAME ]]; then
echo "Usage: bash remove_workflow_runs.sh <USER/ORG NAME> <REPO NAME> <WORKFLOW 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=".workflows[] | select(.name == \"$WORKFLOW_NAME\") | \"\\(.id)\""
echo "Searching for workflow..."
WORKFLOW_ID=$(gh api "repos/$ORG_NAME/$REPO_NAME/actions/workflows?per_page=100" | jq -r "$JQ_SEARCH")
if [ -z $WORKFLOW_ID ]; then
echo "Workflow not found!\nCheck your spelling!"
exit 1;
else
echo "...done!"
fi
echo "Retreiving runs..."
WORKFLOW_RUNS=$(gh api "repos/$ORG_NAME/$REPO_NAME/actions/workflows/$WORKFLOW_ID/runs?per_page=100")
echo "done!"
RUN_COUNT=$(echo $WORKFLOW_RUNS | jq -r ".total_count")
read -r -p "Are you sure you want to delete all $RUN_COUNT runs? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
DELETED=0
LEFT=1
while [[ $LEFT -gt "0" ]]; do
# Be lazy and try to avoid fetching stale results
sleep 1
BATCH=$(gh api "repos/$ORG_NAME/$REPO_NAME/actions/workflows/$WORKFLOW_ID/runs?per_page=100")
LEFT=$(echo $BATCH | jq -r ".total_count")
echo "Deleting up to 100 of $LEFT"
echo $BATCH \
| jq -r '.workflow_runs[] | "\(.id)"' \
| xargs -n1 -I % gh api repos/$ORG_NAME/$REPO_NAME/actions/runs/% -X DELETE
((DELETED=DELETED+LEFT))
echo "done!"
done;
echo ""
echo "All Done! Deleted $DELETED runs from \"$WORKFLOW_NAME\""
else
echo "Writing preliminary data to /tmp/workflow_info.json and aborting!"
echo $WORKFLOW_RUNS > /tmp/workflow_info.json
fi
@e8-HongJunLiu
Copy link

Thank you for your reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment