The GitHub action to delete workflow runs in a repository. This action (written in JavaScript) wraps two Workflow Runs API:
-
List repository workflows -- Lists the workflows in a repository.
-
List workflow runs -- List all workflow runs for a workflow.
-
Delete a workflow run -- Delete a specific workflow run.
The action will calculate the number of days that each workflow run has been retained so far, then use this number to compare with the number you specify for the input parameter "retain_days
". If the retention days of the workflow run has reached (equal to or greater than) the specified number, the workflow run will be deleted.
- Added ability to match multiple values with "
delete_workflow_by_state_pattern
" & "delete_run_by_conclusion_pattern
" by using a comma-separated list - Removed 'all' option from "
delete_workflow_pattern
", simply don't provide it a value for all workflows to be targeted - Added the input parameter "
delete_run_by_conclusion_pattern
" - filters runs by conclusion (useful forskipped
!) - Added the input parameter "
delete_workflow_by_state_pattern
" - filters workflows by state - Added the input parameter "
dry_run
" - only logs targeted workflows & runs, no deletions are performed - Added ability to filter workflows by workflow filename (in addition to the name)
The token used to authenticate.
- If the workflow runs are in the current repository where the action is running, using
github.token
is OK, but you must specify additional permissions within your build job (or at a higher level) to allow the default token access to read the repository contents and to write (delete) action-related data, see the examples below. More details, see theGITHUB_TOKEN
. - If the workflow runs are in another repository, you need to use a personal access token (PAT) that must have the
repo
scope. More details, see "Creating a personal access token".
Name of the repository where the workflow runs are located
Array of repositories where the workflow runs are located. This expects a JSON array.
Amount of days used to compare with the retention days of each workflow
Minimum runs to keep for each workflow
Name or filename of the workflow (if not set, all workflows are targeted)
Filter workflows by state: active, deleted, disabled_fork, disabled_inactivity, disabled_manually
Multiple state values permitted as a comma-separated list
Remove runs based on conclusion: action_required, cancelled, failure, skipped, success
Multiple conclusion values permitted as a comma-separated list
Logs simulated changes, no deletions are performed
If true, the removage of a workflow is skipped, when a run is attached to a existing branch. Set to true avoids that check runs are deleted and the checks are not more present. (excludes main)
If true, the Runs will be checked for linkage to a PR.
In scheduled workflow, see schedule event.
Tip: Using scheduled workflow is the recommended way that can periodically, automatically delete old workflow runs.
name: Delete old workflow runs
on:
schedule:
- cron: '0 0 1 * *'
# Run monthly, at 00:00 on the 1st day of month.
jobs:
del_runs:
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
steps:
- name: Delete workflow runs
uses: Mattraks/delete-workflow-runs@v2
with:
token: ${{ github.token }}
repository: ${{ github.repository }}
retain_days: 30
keep_minimum_runs: 6
name: Delete old workflow runs for all repos in org
on:
schedule:
- cron: '0 0 1 * *'
# Run monthly, at 00:00 on the 1st day of month.
jobs:
del_runs:
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
steps:
- name: Get Accounts List
id: generate-repoList
run: |
echo "repoList=$(gh repo list yourOrgName --json nameWithOwner --jq '[.[].nameWithOwner]')" >> $GITHUB_OUTPUT
- name: Delete workflow runs
uses: 1uphealth/delete-workflow-runs-multi-repo-action
with:
token: ${{ github.token }}
repository_array: ${{ steps.generate-repoList.outputs.repoList }}
retain_days: 30
keep_minimum_runs: 6
In manual triggered workflow, see workflow_dispatch event.
In this way, you can manually trigger the workflow at any time to delete old workflow runs.
name: Delete old workflow runs
on:
workflow_dispatch:
inputs:
days:
description: 'Days-worth of runs to keep for each workflow'
required: true
default: '30'
minimum_runs:
description: 'Minimum runs to keep for each workflow'
required: true
default: '6'
delete_workflow_pattern:
description: 'Name or filename of the workflow (if not set, all workflows are targeted)'
required: false
delete_workflow_by_state_pattern:
description: 'Filter workflows by state: active, deleted, disabled_fork, disabled_inactivity, disabled_manually'
required: true
default: "ALL"
type: choice
options:
- "ALL"
- active
- deleted
- disabled_inactivity
- disabled_manually
delete_run_by_conclusion_pattern:
description: 'Remove runs based on conclusion: action_required, cancelled, failure, skipped, success'
required: true
default: "ALL"
type: choice
options:
- "ALL"
- "Unsuccessful: action_required,cancelled,failure,skipped"
- action_required
- cancelled
- failure
- skipped
- success
dry_run:
description: 'Logs simulated changes, no deletions are performed'
required: false
jobs:
del_runs:
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
steps:
- name: Delete workflow runs
uses: Mattraks/delete-workflow-runs@v2
with:
token: ${{ github.token }}
repository: ${{ github.repository }}
retain_days: ${{ github.event.inputs.days }}
keep_minimum_runs: ${{ github.event.inputs.minimum_runs }}
delete_workflow_pattern: ${{ github.event.inputs.delete_workflow_pattern }}
delete_workflow_by_state_pattern: ${{ github.event.inputs.delete_workflow_by_state_pattern }}
delete_run_by_conclusion_pattern: >-
${{
startsWith(github.event.inputs.delete_run_by_conclusion_pattern, 'Unsuccessful:')
&& 'action_required,cancelled,failure,skipped'
|| github.event.inputs.delete_run_by_conclusion_pattern
}}
dry_run: ${{ github.event.inputs.dry_run }}
The scripts and documentation in this project are released under the MIT License.