gh_delete_workflow_runs

This script will the delete the GitHub Action workflow runs of a given repository.

The following parameters are required:

k

the number of workflow runs to keep (range: [0, n]); older workflow runs will be deleted first

-k 0

will delete all workflow runs.

-k n

if n is greater than the number of existing workflow runs then no runs will be deleted

r

the name of the repository (without the .git extension) for which the workflow runs should be deleted; the name is not case-sensitive

The following environment variable is required:

GH_DELETE_WORKFLOW_RUNS_TOKEN_FILE

the path to the file containing the GitHub access token having the actions:rw permission

Information on how to create an GitHub access token can be found at: GitHub - Creating a fine-grained personal access token

The GitHub API only returns 1000 results—so you might have to execute this script several times if you have more than 1000 workflow runs.

This script does not handle concurrent changes to the workflow runs, i.e. this script might fail if someone else adds or deletes workflow runs while this script is running.

The workflow runs will be irreversibly deleted by this script—​be careful, you have been warned!

Usage

$ GH_DELETE_WORKFLOW_RUNS_TOKEN_FILE=~/.local/secrets/gh/gh-actions-rw scripts/gh/gh_delete_workflow_runs.sh -r sdavids-shell-misc -k 0

WARNING: The following 2 workflow run(s) will be deleted:

[
  {
    "display_title": "feat: 2",
    "created_at": "2024-08-06T13:24:56Z",
    "run_started_at": "2024-08-06T13:24:56Z",
    "html_url": "https://github.com/sdavids/sdavids-shell-misc/actions/runs/90267441015"
  },
  {
    "display_title": "feat: 1",
    "created_at": "2024-08-06T11:52:16Z",
    "run_started_at": "2024-08-06T11:52:16Z",
    "html_url": "https://github.com/sdavids/sdavids-shell-misc/actions/runs/90265969659"
  }
]

Do you really want to irreversibly delete the 2 workflow run(s) (Y/N)? n

$ GH_DELETE_WORKFLOW_RUNS_TOKEN_FILE=~/.local/secrets/gh/gh-actions-rw scripts/gh/gh_delete_workflow_runs.sh -r sdavids-shell-misc -k 1

WARNING: The following 1 workflow run(s) will be deleted:

[
  {
    "display_title": "feat: 1",
    "created_at": "2024-08-06T11:52:16Z",
    "run_started_at": "2024-08-06T11:52:16Z",
    "html_url": "https://github.com/sdavids/sdavids-shell-misc/actions/runs/90265969659"
  }
]

Do you really want to irreversibly delete the 1 workflow run(s) (Y/N)? y

Prerequisites