A GitHub Action to delete workflow runs in a repository. This Action uses JavaScript and interacts with the GitHub API to manage workflow runs efficiently.
- Deletes workflow runs based on retention period and minimum runs to keep.
- Supports filtering by workflow name, filename, state, or run conclusion.
- Includes a dry-run mode to simulate deletions without making changes.
- Skips runs linked to active branches or pull requests (optional).
- Optimized to avoid uploading
node_modulesby bundling code with@vercel/ncc.
- Required: Yes
- Default:
${{ github.token }} - The GitHub token for authentication. Use
github.tokenfor the current repository (requiresactions: writeandcontents: readpermissions) or a Personal Access Token (PAT) withreposcope for other repositories.
- Required: Yes
- Default:
${{ github.repository }} - The repository name in
{owner}/{repo}format.
- Required: Yes
- Default:
30 - Number of days to retain workflow runs before deletion.
- Required: Yes
- Default:
6 - Minimum number of runs to keep per workflow.
- Required: No
- Target workflows by name or filename. Omit to target all workflows.
- Required: No
- Filter workflows by state (comma-separated):
active,deleted,disabled_fork,disabled_inactivity,disabled_manually.
- Required: No
- Filter runs by conclusion (comma-separated):
action_required,cancelled,failure,skipped,success.
- Required: No
- Default:
false - Simulate deletions and log actions without performing them.
- Required: No
- Default:
false - Skip deletion if the run is linked to an existing branch (excludes
main).
- Required: No
- Default:
false - Skip deletion if the run is linked to a pull request.
- Ensure the repository has a
package.jsonwith dependencies and a build script using@vercel/ncc. - Run
npm installandnpm run buildto generatedist/index.js. - Commit the
dist/folder, but excludenode_modules/using.gitignore. - Use the Action in your workflow as shown below.
Run monthly to delete old workflow runs:
name: Delete old workflow runs
on:
schedule:
- cron: "0 0 1 * *" # Monthly at 00:00 on the 1st
jobs:
delete-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: 6Trigger manually with customizable inputs:
name: Delete old workflow runs
on:
workflow_dispatch:
inputs:
days:
description: "Days to retain runs"
required: true
default: "30"
minimum_runs:
description: "Minimum runs to keep"
required: true
default: "6"
delete_workflow_pattern:
description: "Workflow name or filename (omit for all)"
required: false
delete_workflow_by_state_pattern:
description: "Workflow state: active, deleted, disabled_fork, disabled_inactivity, disabled_manually"
required: false
default: "ALL"
type: choice
options:
- "ALL"
- active
- deleted
- disabled_inactivity
- disabled_manually
delete_run_by_conclusion_pattern:
description: "Run conclusion: action_required, cancelled, failure, skipped, success"
required: false
default: "ALL"
type: choice
options:
- "ALL"
- "Unsuccessful: action_required,cancelled,failure,skipped"
- action_required
- cancelled
- failure
- skipped
- success
dry_run:
description: "Simulate deletions"
required: false
default: "false"
type: choice
options:
- "false"
- "true"
jobs:
delete-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 }}For GitHub Enterprise, specify the API base URL:
jobs:
delete-runs:
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
steps:
- name: Delete old workflow runs
uses: Mattraks/delete-workflow-runs@v2
with:
token: ${{ secrets.PAT_TOKEN }}
baseUrl: https://github.mycompany.com/api/v3
repository: mycompany/myrepo
retain_days: 30
keep_minimum_runs: 6To build the Action:
- Install dependencies:
npm install - Build the Action:
npm run build - Commit the
dist/folder to the repository.
The node_modules folder is excluded via .gitignore to reduce repository size.
This project is licensed under the MIT License.