Sync recursive delete folder - Node.js File

Node.js examples for File:Directory

Description

Sync recursive delete folder

Demo Code

var path = require('path');
var fs = require('fs');
var crypto = require('crypto');

/*sync recursive delete folder*/
exports.rmdirSync = function(p) {
    if(fs.existsSync(p)){
      var stat = fs.statSync(p);
      if(stat.isDirectory()){
        var files = fs.readdirSync(p);
        files.forEach(function(file) {
              var fullName = path.join(p, file);
              if (fs.statSync(fullName).isDirectory()) {
                  exports.rmdirSync(fullName);
              } else {/*from   ww  w  .  ja  v a 2 s  . c om*/
                  fs.unlinkSync(fullName);
              }
          });
        fs.rmdirSync(p);
      }else{
        fs.unlinkSync(p);
      }
    }
}

Related Tutorials