Created
November 7, 2024 15:09
-
-
Save coyotte508/37692f06936070a75b41afe90b86fd00 to your computer and use it in GitHub Desktop.
Delete github workflow runs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bun run delete-runs.ts | |
export {}; | |
// Github access token | |
const key = "ghp_..."; | |
const repo = "user/repo"; | |
const jobName = "job-name"; | |
// Step 1: Get all runs | |
let url: string | undefined = | |
`https://api.github.com/repos/${repo}/actions/runs`; | |
const runIds: string[] = []; | |
while (url) { | |
const resp = await fetch(url, { | |
headers: { | |
Authorization: `token ${key}`, | |
"X-Github-Api-Version": "2022-11-28", | |
}, | |
}); | |
if (!resp.ok) { | |
console.error(resp.statusText); | |
console.error(await resp.text()); | |
break; | |
} | |
const linkHeader = resp.headers.get("link"); | |
console.log("Link", linkHeader); | |
url = linkHeader?.match(/<([^>]+)>; rel="next"/)?.[1]; | |
runIds.push( | |
...(await resp | |
.json() | |
.then((data) => data.workflow_runs) | |
.then((runs) => | |
runs.filter((run) => run.name === jobName).map((run) => run.id), | |
)), | |
); | |
} | |
console.log("Found", runIds.length, "runs"); | |
for (const runId of runIds) { | |
console.log("Deleting", runId); | |
// Delete run | |
const resp = await fetch( | |
`https://api.github.com/repos/${repo}/actions/runs/${runId}`, | |
{ | |
method: "DELETE", | |
headers: { | |
Authorization: `token ${key}`, | |
"X-Github-Api-Version": "2022-11-28", | |
}, | |
}, | |
); | |
if (!resp.ok) { | |
console.error(resp.statusText); | |
console.error(await resp.text()); | |
} | |
} | |
console.log("done"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment