Skip to content

Stale Workflow Queue Cleanup #31

Stale Workflow Queue Cleanup

Stale Workflow Queue Cleanup #31

name: Stale Workflow Queue Cleanup
on:
workflow_dispatch:
schedule:
# everyday at 15:00
- cron: '0 15 * * *'
permissions:
contents: read
concurrency:
group: stale-workflow-queue-cleanup
cancel-in-progress: true
jobs:
cleanup:
name: Cleanup
runs-on: ubuntu-24.04
timeout-minutes: 10
if: github.ref == 'refs/heads/main'
permissions:
actions: write # to delete stale workflow runs
steps:
- name: Delete stale queued workflow runs
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
retries: 3
script: |
// Remove any workflow runs in "queued" state for more than 1 day
const cutoff = Date.now() - 24 * 60 * 60 * 1000;
const runs = await github.paginate(
github.rest.actions.listWorkflowRunsForRepo,
{ ...context.repo, status: "queued", per_page: 100 },
);
for (const run of runs.filter((r) => Date.parse(r.created_at) < cutoff)) {
core.info(`Deleting workflow run ${run.id}`);
try {
await github.rest.actions.deleteWorkflowRun({ ...context.repo, run_id: run.id });
} catch (err) {
core.warning(`Failed to delete workflow run ${run.id}: ${err.message}`);
}
}