lib/fileSystem.js
function rmdirRecursive(directoryPath, callback) {

  // Open directory
  fs.readdir(directoryPath, function(error, resources) {

    // Failed reading directory
    if (error)
      return callback(error);

    var pendingResourceNumber = resources.length;

    // No more pending resources, done for this directory
    if (!pendingResourceNumber) {

      // Remove directory
      fs.rmdir(directoryPath, callback);

    }

    // Iterate through the list of resources in the directory
    resources.forEach(function(resource) {

      var resourcePath = path.join(directoryPath, resource);

      // Get resource stats
      fs.stat(resourcePath, function(error, stats) {
        if (error)
          return callback(error);

        // Resource correspond to a directory
        if (stats.isDirectory()) {

          resources = rmdirRecursive(path.join(directoryPath, resource), function(error) {
            if (error)
              return callback(error);

            pendingResourceNumber--;

            if (!pendingResourceNumber)
              fs.rmdir(directoryPath, callback);

          });

        } else {

          // Resource does not correspond to a directory
          // Mark resource as treated

          // Remove file
          fs.unlink(resourcePath, function(error) {
            if (error)
              return callback(error);
            else {
              pendingResourceNumber--;

              if (!pendingResourceNumber)
                fs.rmdir(directoryPath, callback);

            }
          });

        }

      });

    });

  });

}
function rmdirRecursive(directoryPath, callback) {

  // Open directory
  fs.readdir(directoryPath, function(error, resources) {

    // Failed reading directory
    if (error)
      return callback(error);

    var pendingResourceNumber = resources.length;

    // No more pending resources, done for this directory
    if (!pendingResourceNumber) {

      // Remove directory
      fs.rmdir(directoryPath, callback);

    }

    // Iterate through the list of resources in the directory
    resources.forEach(function(resource) {

      var resourcePath = path.join(directoryPath, resource);

      // Get resource stats
      fs.stat(resourcePath, function(error, stats) {
        if (error)
          return callback(error);

        // Resource correspond to a directory
        if (stats.isDirectory()) {

          resources = rmdirRecursive(path.join(directoryPath, resource), function(error) {
            if (error)
              return callback(error);

            pendingResourceNumber--;

            if (!pendingResourceNumber)
              fs.rmdir(directoryPath, callback);

          });

        } else {

          // Resource does not correspond to a directory
          // Mark resource as treated

          // Remove file
          fs.unlink(resourcePath, function(error) {
            if (error)
              return callback(error);
            else {
              pendingResourceNumber--;

              if (!pendingResourceNumber)
                fs.rmdir(directoryPath, callback);

            }
          });

        }

      });

    });

  });

}
function rmdirRecursive ( directoryPath , callback ) { // Open directory fs . readdir ( directoryPath , function ( error , resources ) { // Failed reading directory if ( error ) return callback ( error ) ; var pendingResourceNumber = resources . length ; // No more pending resources, done for this directory if ( ! pendingResourceNumber ) { // Remove directory fs . rmdir ( directoryPath , callback ) ; } // Iterate through the list of resources in the directory resources . forEach ( function ( resource ) { var resourcePath = path . join ( directoryPath , resource ) ; // Get resource stats fs . stat ( resourcePath , function ( error , stats ) { if ( error ) return callback ( error ) ; // Resource correspond to a directory if ( stats . isDirectory ( ) ) { resources = rmdirRecursive ( path . join ( directoryPath , resource ) , function ( error ) { if ( error ) return callback ( error ) ; pendingResourceNumber -- ; if ( ! pendingResourceNumber ) fs . rmdir ( directoryPath , callback ) ; } ) ; } else { // Resource does not correspond to a directory // Mark resource as treated // Remove file fs . unlink ( resourcePath , function ( error ) { if ( error ) return callback ( error ) ; else { pendingResourceNumber -- ; if ( ! pendingResourceNumber ) fs . rmdir ( directoryPath , callback ) ; } } ) ; } } ) ; } ) ; } ) ; }
Removes a directory and all its content recursively and asynchronously.

It is assumed that the directory exists.

@method rmdirRecursive
@private
@static
@async
@param {String} directoryPath Path of the directory to remove
@param {Function} callback The function to call when done
- **Error** The error if an error occurred, null otherwise
Removes a directory and all its content recursively and asynchronously .