Clean Old Github Action Runs


Warning: This is a destructive operation. Proceed with caution!

For a non destructive test run, you can comment out the xargs delete call.

Code

#!/usr/bin/env bash

export OWNER="<owner>"
export REPOSITORY="<repo>"
export WORKFLOW="<workflow-name>"

gh api -X GET /repos/$OWNER/$REPOSITORY/actions/runs --paginate \
	| jq '.workflow_runs[] | select(.name == '\"$WORKFLOW\"') | .id' \
	| xargs -t -I{} gh api -X DELETE /repos/$OWNER/$REPOSITORY/actions/runs/{} --silent

Start by exporting three variables that defines a path to the github action workflow run list on github.com.

Using the Github CLI, we can paginate through the specific workflow runs by adding --paginate. This will list all the runs against that repository so we need to filter it down to the workflow that we specify.

You can use jq to query the json response string to look for workflow_runs that has the name we set in one of our variables: WORKFLOW & only select the ID from that response.

Pipe this ID into a xargs call to delete the specific run. Use --silent to allow the Github CLI to not wait for a keypress to continue and it'll loop over all the workflow runs that it can find.

Getting Started

brew install gh jq
gh auth login

Crediting the Source

Thanks to David Miguel for his SO post. This little bash helper helped to clean up thousands of stale runs that our team used in testing & various migration tasks.