Skip to content

Instantly share code, notes, and snippets.

@seleb
Created March 19, 2018 02:42
Show Gist options
  • Save seleb/ccdba97034c6820021bc57894df59db0 to your computer and use it in GitHub Desktop.
Save seleb/ccdba97034c6820021bc57894df59db0 to your computer and use it in GitHub Desktop.
Deletes all files in a directory with a given extension
const path = require("path");
const fs = require("fs");
function clearDir(dirPath, ext) {
fs.readdirSync(dirPath)
.filter(filePath => path.extname(filePath) === ext)
.forEach(filePath => {
filePath = path.join(dirPath, filePath);
try {
fs.unlinkSync(filePath);
} catch (e) {
console.error(e);
}
})
};
// example
clearDir(path.resolve("./output", ".png"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment