Skip to content

Instantly share code, notes, and snippets.

@sguzunov
Created August 12, 2017 14:01
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 sguzunov/b7c13eb02a4d6a1fd16cc34ad7bc092f to your computer and use it in GitHub Desktop.
Save sguzunov/b7c13eb02a4d6a1fd16cc34ad7bc092f to your computer and use it in GitHub Desktop.
function clearDirectorySync(dirAbsPath) {
const fs = require('fs'); // Node.js package for working with file system (required).
const rmDirContent = function rmDir(dirAbsPath) {
if (!fs.existsSync(dirAbsPath)) {
return;
}
const dirContent = fs.readdirSync(dirAbsPath);
if (!dirContent.length) return;
dirContent.forEach((el) => {
const pathToElement = `${dirAbsPath}/${el}`;
const isDirectory = fs.lstatSync(pathToElement).isDirectory();
if (isDirectory) {
rmDir(pathToElement);
fs.rmdirSync(pathToElement);
} else {
fs.unlinkSync(pathToElement);
}
});
};
rmDirContent(dirAbsPath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment