Skip to content

Instantly share code, notes, and snippets.

@aaditkamat
Last active July 16, 2023 23:02
Show Gist options
  • Save aaditkamat/5425254771df96888c379de05d1ae7f3 to your computer and use it in GitHub Desktop.
Save aaditkamat/5425254771df96888c379de05d1ae7f3 to your computer and use it in GitHub Desktop.
Delete unsuccessful repository workflows
# Install brew if not already installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install the JSON processing command line utility
brew install jq
# List workflow runs for a repository
runs=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/$GITHUB_REPO_OWNER/$GITHUB_REPO_NAME/actions/runs) # Make sure to initalize the variables before running the script
# Get a list of ids for all the unsuccessful workflow runs
num_runs=$(echo $runs | jq '.total_count')
workflow_runs=$(echo $runs | jq '.workflow_runs[]')
run_ids_to_delete=()
for i in {1..workflow_runs}; do
run=$(echo $workflow_runs | jq ".[$i]")
if [ $(echo $run | jq '.conclusion') == '"failed"' ]; then
run_ids_to_delete+=($(echo $run | jq '.id'))
fi
done
# Delete the unsuccessful workflow runs
for run_id in "${run_ids_to_delete[@]}"; do
echo "Deleting run with id $run_id"
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-X DELETE \
/repos/$GITHUB_REPO_OWNER/$GITHUB_REPO_NAME/actions/runs/$run_id
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment