Last active
February 3, 2021 12:36
-
-
Save geedew/cf66b81b0bcdab1f334b to your computer and use it in GitHub Desktop.
Removing a directory that is not empty in NodeJS
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
var fs = require('fs'); | |
var deleteFolderRecursive = function(path) { | |
if( fs.existsSync(path) ) { | |
fs.readdirSync(path).forEach(function(file,index){ | |
var curPath = path + "/" + file; | |
if(fs.lstatSync(curPath).isDirectory()) { // recurse | |
deleteFolderRecursive(curPath); | |
} else { // delete file | |
fs.unlinkSync(curPath); | |
} | |
}); | |
fs.rmdirSync(path); | |
} | |
}; |
Also this code would be a minified version of yours:
function rmdir(d) {
var self = arguments.callee
if (fs.existsSync(d)) {
fs.readdirSync(d).forEach(function(file) {
var C = d + '/' + file
if (fs.statSync(C).isDirectory()) self(C)
else fs.unlinkSync(C)
})
fs.rmdirSync(d)
}
}
If you could fix that(or yours) it would help me a lot
@elpeleq42 this might help you 👍
https://gist.github.com/laazebislam/845e61a18541255c0a29eeeb4e0141f4
const { execSync } = require('child_process')
const path = require('path')
const DIR = path.resolve(__dirname, '../directory-to-delete')
if (process.platform === 'win32') {
execSync(`del /f /s ${DIR}`)
} else {
execSync(`rm -rf ${DIR}`)
}
This works for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesn't work. Says that the directory is not empty