Delete Folder Recursively (Node.js)

JS
S
JavaScript

Node.js script to delete a folder including subfolders and files

1
2import fs from 'fs-extra'
3
4// Function
5function deleteFolderRecursively(path) {
6    if (fs.existsSync(path)) {
7        fs.readdirSync(path).forEach(function iterateFile(file) {
8            var curPath = `${path}/${file}`
9            if (fs.lstatSync(curPath).isDirectory()) {
10                _deleteFolderRecursive(curPath);
11            } else {
12                fs.unlinkSync(curPath);
13            }
14        })
15        fs.rmdirSync(path);
16    }
17}
18
19// Usage
20deleteFolderRecursively('./tmp');

Created on 9/17/2018