Skip to content

Instantly share code, notes, and snippets.

@Siddhant-K-code
Created March 30, 2024 08:17
Show Gist options
  • Save Siddhant-K-code/95f35167dab50652eb7be7cdd23f0d96 to your computer and use it in GitHub Desktop.
Save Siddhant-K-code/95f35167dab50652eb7be7cdd23f0d96 to your computer and use it in GitHub Desktop.
Clearing workflow runs for GitHub actions
const owner = "repo-owner";
const repo = "repo-name";
const fileName = "your-workflow-file.yml";
async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function deleteRuns() {
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}/actions/workflows/${fileName}/runs?per_page=100`,
{
headers: {
// Include necessary headers here, such as Authorization if needed
Authorization: "Bearer YOUR_TOKEN_HERE",
Accept: "application/vnd.github.v3+json",
},
}
);
if (!response.ok) {
console.error(`Failed to get workflow runs: ${response.statusText}`);
return;
}
const data = await response.json();
const runIds = data.workflow_runs.map((run: { id: number }) => run.id);
for (const runId of runIds) {
const deleteResponse = await fetch(
`https://api.github.com/repos/${owner}/${repo}/actions/runs/${runId}`,
{
method: "DELETE",
headers: {
Authorization: "Bearer YOUR_TOKEN_HERE",
Accept: "application/vnd.github.v3+json",
},
}
);
if (!deleteResponse.ok) {
console.error(
`Failed to delete run ${runId}: ${deleteResponse.statusText}`
);
}
}
}
async function main() {
while (true) {
await deleteRuns();
await sleep(1000);
}
}
main().catch(console.error);
/**
* Important Notes:
* Replace 'YOUR_TOKEN_HERE' with your actual GitHub API token for authorization.
* Replace 'owner' and'repo' with the actual owner and repository names of your GitHub repository.
* Replace 'fileName' with the actual name of your workflow file.
* The fetch function is used for making HTTP requests, assuming a modern environment where fetch is available. If you're using this in an environment where fetch isn't natively available (like older versions of Node.js), you might need to use a library like axios or polyfill fetch.
* The infinite loop in the main function will continuously delete workflow runs, waiting 1 second between each batch of deletions.
* Make sure to have a strategy for breaking out of this loop or handling errors properly to avoid unintended consequences.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment