Last active
August 10, 2023 08:26
-
-
Save samueljseay/b6100fe1d7007bd6c48b900f1e61834a to your computer and use it in GitHub Desktop.
Delete runs for a workflow using GH cli / node.js
This file contains 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
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