Delete all the files in a directory, applying an optional filter
@param {string} directory path of the directory to clean
@returns {Promise<array>} resolve with all the files deleted succesfully
330318
function deleteDirectoryFiles(directory, filter = () => true) {
  return readDirectoryFiles(directory)
    .then((files) => {
      const quietDelete = f => deleteFile(path.join(directory, f)).catch(() => null);
      const deletingFiles = files.filter(filter).map(quietDelete);
      return Promise.all(deletingFiles).then(deleted => deleted.filter(d => d !== null));
    });
}
987054
function getDirectoryTree(dir) {
    const items = [];
    return new Promise(function(resolve) {
        fs.walk(dir)
        .on('data', function (item) {
            items.push(item.path);
        })
        .on('end', function () {
            resolve(items);
        });
    });
}
1278323
function cleanup_directory(directory_path, callback) {
  // only remove the folder at directory_path if it exists
  fs.exists(directory_path, function (exists) {
    if (!exists) {
      return callback();
    }
    rimraf(directory_path, callback);
  });
}
694837
public static void cleanUpDir(final File dir) {
    try {
      if (dir != null && dir.exists()) {
        FileUtils.deleteDirectory(dir);
      }
    } catch (final IOException e) {
      logger.error("Failed to delete the directory", e);
      dir.deleteOnExit();
    }
  }
272716
public final void removeDirectory(final File directory) {
        try {
            FileUtils.deleteDirectory(directory);
        } catch (IOException e) {
            LOGGER.error("Unable to delete directory '{}'", directory);
        }
    }
598388
function deleteOldFiles(path){

      var deferred = Q.defer();

      // Don't delete existing files & folders if overwrite is off
      if(options.overWrite !== false){

        grunt.log.writeln('---> Deleting folder ==> '+path);

        rimraf(path,  function(err){
          if(err) {
            grunt.log.warn('---> There was an error deleting the existing repo folder : '+path+', error : '+err);
            deferred.reject(err);
          }else{
            deferred.resolve();
          }
        });

      }else{
        if(grunt.file.isDir(path)){
          grunt.log.writeln('---> Folder exists so plugin will not overwrite ==> '+path);
        }
        deferred.resolve();
      }

      return deferred.promise;

    }
663798
public static void recursivelyDeleteDirectoryOnExit(final File directory) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          recursivelyDeleteDirectory(directory);
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    });
  }
264769
public static void purgeDirectory(File directory) {

        if (directory.canRead() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                if (f.isDirectory()) {
                    purgeDirectory(f);
                }
                if (f.canWrite()) {
                    f.delete();
                }
            }
            directory.delete();
        }
    }
1254020
function readDirectoryFiles(directory) {
  return new Promise((resolve, reject) => {
    fs.readdir(directory, (err, files) => {
      if (err) {
        reject(err);
        return;
      }

      const isFile = fileName => readFileStats(path.join(directory, fileName))
        .then((fstat) => {
          if (fstat.isFile()) {
            return fileName;
          }
          return null;
        });

      Promise.all(files.map(isFile))
        .then(fileList => resolve(fileList.filter(f => f !== null)))
        .catch(reject);
    });
  });
}
390279
static function cleanupEmptyDirectories( $dir )
    {
        $dir = self::cleanPath( $dir, self::SEPARATOR_UNIX );
        $dirElements = explode( '/', $dir );
        if ( count( $dirElements ) == 0 )
            return true;
        $currentDir = $dirElements[0];
        $result = true;
        if ( !file_exists( $currentDir ) and $currentDir != "" )
            $result = self::doMkdir( $currentDir, self::directoryPermission() );
        if ( !$result )
            return false;

        for ( $i = count( $dirElements ); $i > 0; --$i )
        {
            $dirpath = implode( '/', array_slice( $dirElements, 0, $i ) );
            if ( file_exists( $dirpath ) and
                 is_dir( $dirpath ) )
            {
                $rmdirStatus = @rmdir( $dirpath );
                if ( !$rmdirStatus )
                    return true;
            }
        }
        return true;
    }
589075
function resolve(files, opts) {
  var fn = opts.filter || function(fp) {
    return true;
  };

  if (opts.realpath === true) {
    return files.filter(fn);
  }

  var len = files.length;
  var idx = -1;
  var res = [];

  while (++idx < len) {
    var fp = path.resolve(opts.cwd, files[idx]);
    if (!fn(fp) || ~res.indexOf(fp)) continue;
    res.push(fp);
  }
  return res;
}
684802
function deleteRecursive (kb, folder) {
        return new Promise(function (resolve, reject) {
          kb.fetcher.load(folder).then(function () {
            let promises = kb.each(folder, ns.ldp('contains')).map(file => {
              if (kb.holds(file, ns.rdf('type'), ns.ldp('BasicContainer'))) {
                return deleteRecursive(kb, file)
              } else {
                console.log('deleteRecirsive file: ' + file)
                if (!confirm(' Really DELETE File ' + file)) {
                  throw new Error('User aborted delete file')
                }
                return kb.fetcher.webOperation('DELETE', file.uri)
              }
            })
            console.log('deleteRecirsive folder: ' + folder)
            if (!confirm(' Really DELETE folder ' + folder)) {
              throw new Error('User aborted delete file')
            }
            promises.push(kb.fetcher.webOperation('DELETE', folder.uri))
            Promise.all(promises).then(res => { resolve() })
          })
        })
      }
1342867
function directoryCleanEverything(dirPath, includeExtension, toRemove, replacement) {
    if(!fs.existsSync(dirPath)) return;
    let isDirectory = fs.statSync(dirPath).isDirectory();
    // try cleaning package.json in current dir
    if(!isDirectory && dirPath.match(new RegExp(includeExtension + "$", "g"))) {
        let content = fs.readFileSync(dirPath).toString();
        while(content.indexOf(toRemove) >= 0) {
            content = content.replace(toRemove, replacement || "");
        }
        fs.writeFileSync(dirPath, content);
    }
    else if(isDirectory) {
        // check for subdirectories
        let list = fs.readdirSync(dirPath);
        for(let dir of list) {
            let subDirPath = dirPath + "/" + dir;
            directoryCleanEverything(subDirPath, includeExtension, toRemove, replacement);
        }
    }
}
645391
function makeDir(file) {
  return new Promise((resolve, reject) => {
    mkdirp(path.extname(file) ? path.dirname(file) : file, (err) => {
      if (err) reject(err);
      resolve();
    });
  });
}
1418
function resolveFile (dir, file) {
  return new Promise((resolve, reject) => {
    resolver.resolve({}, dir, file, (err, result) => {
      if (err) reject(err)
      else resolve(result)
    })
  })
}
641007
function cleanFolder(outPath) {
    try {
        var stat = fs.lstatSync(outPath);
        if (stat.isDirectory()) {
            wrench.rmdirSyncRecursive(outPath);
        } else  {
            fs.unlinkSync(outPath);
        }
    } catch (e) {
        if (e.code != "ENOENT") throw e;
    }
}
952454
function cleanFolder(root) {
    if (!fs.existsSync(root)) return fsUtils.mkdirp(root);

    return listFiles(root, {
        ignoreFiles: [],
        ignoreRules: [
            // Skip Git and SVN stuff
            '.git/',
            '.svn/'
        ]
    })
    .then(function(files) {
        var d = Q.defer();

        _.reduce(files, function(prev, file, i) {
            return prev.then(function() {
                var _file = path.join(root, file);

                d.notify({
                    i: i+1,
                    count: files.length,
                    file: _file
                });
                return fsUtils.remove(_file);
            });
        }, Q())
        .then(function() {
            d.resolve();
        }, function(err) {
            d.reject(err);
        });

        return d.promise;
    });
}