Skip to content

Instantly share code, notes, and snippets.

@samueljseay
Last active August 10, 2023 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samueljseay/b6100fe1d7007bd6c48b900f1e61834a to your computer and use it in GitHub Desktop.
Save samueljseay/b6100fe1d7007bd6c48b900f1e61834a to your computer and use it in GitHub Desktop.
Delete runs for a workflow using GH cli / node.js
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
function deleteRuns(workflowName, repoNameWithOwner) {
try {
// Get the list of run IDs
execSync(`gh run list --json databaseId -w ${workflowName} -L 9999 -q '.[].databaseId' > temp.txt`);
// Read and process each run ID
const runIds = fs.readFileSync('temp.txt', 'utf8').trim().split('\n');
console.log(`Deleting ${runIds.length} runs...`);
runIds.forEach((runId, i) => {
console.log(`Deletion #${i} of ${runIds.length}. Deleting run with ID: ${runId}`);
execSync(`gh api "repos/${repoNameWithOwner}/actions/runs/${runId}" -X DELETE`);
console.log(`Deleted run with ID: ${runId}`);
});
// Clean up temporary file
fs.unlinkSync('temp.txt');
} catch (error) {
console.error('An error occurred:', error);
}
}
// Check if the workflow name and repo name with owner are provided
if (process.argv.length !== 4) {
console.log(`Usage: node ${path.basename(__filename)} <workflow-name> <repo-name-with-owner>`);
process.exit(1);
}
const workflowName = process.argv[2];
const repoNameWithOwner = process.argv[3];
deleteRuns(workflowName, repoNameWithOwner);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment