Batch remove GHA workflow runs and logs

2 minute read

If you remove a GHA workflow from a repo and there is a history of runs, it doesn’t actually remove it from the Github UI until you clean up the old runs. This is a script to remove all runs and/or logs for a given workflow. Your Github organization will need to be set on the endpoint or removed entirely if not part of an organization.

Before running, you will need to login through the Github CLI first.

1gh auth login
 1#!/usr/bin/env bash
 2
 3# Delete all logs or runs for a given workflow
 4# Usage: workflow_cleanup.sh <repository> <workflow-name> <job (logs or runs)>
 5
 6set -oe pipefail
 7
 8REPOSITORY=$1
 9WORKFLOW_NAME=$2
10JOB=$3
11
12# Validate arguments
13if [[ -z "$REPOSITORY" ]]; then
14  echo "Repository is required"
15  exit 1
16fi
17
18if [[ -z "$WORKFLOW_NAME" ]]; then
19  echo "Workflow name is required"
20  exit 1
21fi
22
23echo "Getting all completed runs for workflow $WORKFLOW_NAME in $REPOSITORY"
24
25RUNS=$(
26  gh api \
27    -H "Accept: application/vnd.github+json" \
28    -H "X-GitHub-Api-Version: 2022-11-28" \
29    "/repos/<org>/$REPOSITORY/actions/workflows/$WORKFLOW_NAME/runs" \
30    --paginate \
31    --jq '.workflow_runs[] | select(.conclusion != "") | .id'
32)
33
34echo "Found $(echo "$RUNS" | wc -l) completed runs for workflow $WORKFLOW_NAME"
35
36# Delete logs for each run
37if [ "$JOB" == "logs" ]; then
38  for RUN in $RUNS; do
39    echo "Deleting logs for run $RUN"
40    gh api \
41      --silent \
42      --method DELETE \
43      -H "Accept: application/vnd.github+json" \
44      -H "X-GitHub-Api-Version: 2022-11-28" \
45      "/repos/<org>/$REPOSITORY/actions/runs/$RUN/logs" || echo "Failed to delete logs for run $RUN"
46
47    # Sleep for 100ms to avoid rate limiting
48    sleep 0.1
49  done
50fi
51
52# Delete all runs for a workflow
53if [ "$JOB" == "runs" ]; then
54  for RUN in $RUNS; do
55    echo "Deleting run $RUN"
56    gh api \
57      --silent \
58      --method DELETE \
59      -H "Accept: application/vnd.github+json" \
60      -H "X-GitHub-Api-Version: 2022-11-28" \
61      "/repos/<org>/$REPOSITORY/actions/runs/$RUN" \
62
63    # Sleep for 100ms to avoid rate limiting
64    sleep 0.1
65  done
66fi

I also had an issue with a workflow step that is always set to run and was set to the a runner type that doesn’t exist (self-hosted runners) and there was no way to stop it through the UI. This is a script to force stop that job. Your Github organization will need to set on the endpoint or removed entirely if not part of an organization.

 1#!/usr/bin/env bash
 2
 3# Force cancel a workflow. Jobs that have an "always" run flag are not cancellable through the UI.
 4# Usage: workflow_force_stop.sh <repository> <run_id>
 5
 6set -oe pipefail
 7
 8REPOSITORY=$1
 9RUN_ID=$2
10
11# Validate arguments
12if [[ -z "$REPOSITORY" ]]; then
13  echo "Repository is required"
14  exit 1
15fi
16
17if [[ -z "$RUN_ID" ]]; then
18  echo "Run ID is required"
19  exit 1
20fi
21
22gh api \
23  --silent \
24  --method POST \
25  -H "Accept: application/vnd.github+json" \
26  -H "X-GitHub-Api-Version: 2022-11-28" \
27  "/repos/<org>/$REPOSITORY/actions/runs/$RUN_ID/force-cancel"