Skip to content

Instantly share code, notes, and snippets.

@dsmabulage
Last active September 24, 2023 11:30
Show Gist options
  • Save dsmabulage/94ca96a0602df60f6872edac1db11d41 to your computer and use it in GitHub Desktop.
Save dsmabulage/94ca96a0602df60f6872edac1db11d41 to your computer and use it in GitHub Desktop.
Delete Multiple GitHub workflows via APIs
// First generate a personal access token for your account
// https://docs.github.com/en/[email protected]/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
// For every APi request send the token as a bearer token
// HTTP GET
https://api.github.com/repos/{account_name}/{repo_name}/actions/workflows
// This will return all the workflows in the selected repository
// HTTP GET
https://api.github.com/repos/{account_name}/{repo_name}/actions/workflows/{workflow_id}/runs
// Grab the workflow id from previous API response and view all the runs within that workflow
// HTTP DELETE
https://api.github.com/repos/{account_name}/{repo_name}/actions/runs/{workflow_run_id}
// delete the workflow run with id no
const GITHUB_USERNAME = 'username';
const GITHUB_REPO = 'repo_name';
const WORKFLOWS_URL = `https://api.github.com/repos/${GITHUB_USERNAME}/${GITHUB_REPO}/actions`;
try {
// Get all workflow ids
const workflowIds = await axios.get(`${WORKFLOWS_URL}/workflows`, {
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
});
const allWorkflowIds = workflowIds.data.workflows.map(
(workflow) => workflow.id
);
console.log({ allWorkflowIds });
// Get all workflow runs
const workflowRuns = await Promise.all(
allWorkflowIds.map(
async (workflowId) =>
await axios.get(`${WORKFLOWS_URL}/workflows/${workflowId}/runs`, {
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
})
)
);
// Delete all workflow runs
const deletedRuns = await Promise.all(
workflowRuns.map(async (workflowRun) => {
const workflowRunIds = workflowRun.data.workflow_runs.map(
(workflow) => workflow.id
);
return await Promise.all(
workflowRunIds.map(
async (workflowRunId) =>
await axios.delete(`${WORKFLOWS_URL}/runs/${workflowRunId}`, {
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
})
)
);
})
);
} catch (error) {
console.log(error);
}
// delete all workflow runs
try {
const allWorkflowRunsIds = await axios.get(`${WORKFLOWS_URL}/runs`, {
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
});
const workflowRunsIds = allWorkflowRunsIds.data.workflow_runs.map(
(workflow) => workflow.id
);
// Delete all workflow runs
const deletedRuns = await Promise.all(
workflowRunsIds.map(
async (workflowRunId) =>
await axios.delete(`${WORKFLOWS_URL}/runs/${workflowRunId}`, {
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
})
)
);
} catch (error) {
console.log(error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment