Skip to content

Instantly share code, notes, and snippets.

@sylwit
Last active April 21, 2023 21:06
Show Gist options
  • Save sylwit/ecf67291dbc54bab628dd03771bac6fd to your computer and use it in GitHub Desktop.
Save sylwit/ecf67291dbc54bab628dd03771bac6fd to your computer and use it in GitHub Desktop.
This bash script deletes all the runs from a Github Actions workflow, since you can't delete a workflow from the UI
#!/bin/bash
# Set the repository owner and name
owner="<owner>"
repo="<repo>"
# Replace <YOUR-PAT> with your actual Personal Access Token PAT
PAT="<YOUR-PAT>"
auth_curl() { curl -s -H "Authorization: Bearer $PAT" "$@"; }
# Get the workflow name from the command line argument or from user input
if [ -z "$1" ]; then
auth_curl "https://api.github.com/repos/$owner/$repo/actions/workflows" | jq -r ".workflows[].name"
read -rp "Enter the workflow name: " workflow_name
else
workflow_name="$1"
fi
# Get the workflow ID using the GitHub API
workflow_id=$(auth_curl "https://api.github.com/repos/$owner/$repo/actions/workflows" | jq -r ".workflows[] | select(.name == \"$workflow_name\") | .id")
if [ -z "$workflow_id" ]; then
echo "Workflow not found."
exit 1
fi
# Get a list of all the workflow runs using the GitHub API
workflow_runs=$(auth_curl "https://api.github.com/repos/$owner/$repo/actions/workflows/$workflow_id/runs?per_page=100" | jq -r ".workflow_runs[] | .id")
# Loop through the workflow runs and delete them using the GitHub API
for run_id in $workflow_runs; do
echo "Deleting run $run_id..."
auth_curl -X DELETE "https://api.github.com/repos/$owner/$repo/actions/runs/$run_id"
done
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment