Java Utililty Methods Directory Delete nio

List of utility methods to do Directory Delete nio

Description

The list of methods to do Directory Delete nio are organized into topic(s).

Method

voiddeleteDirectory(File directory)
delete Directory
cleanDirectory(directory);
Files.delete(Paths.get(directory.getAbsolutePath()));
voiddeleteDirectory(File directory)
Recursively delete a directory and all its contents
if (directory.exists()) {
    File[] files = directory.listFiles();
    if (null != files) {
        for (File file : files) {
            if (file.isDirectory()) {
                deleteDirectory(file);
            } else {
                Files.deleteIfExists(file.toPath());
...
BooleandeleteDirectory(File file)
Method to delete entire directories recursively.
file.setWritable(true, false);
file.setExecutable(true, false);
file.setReadable(true, false);
if (file.isDirectory()) {
    for (File f : file.listFiles()) {
        deleteDirectory(f);
System.gc();
Boolean deleted = file.delete();
;
if (!deleted) {
    Path path = file.toPath();
    try {
        Files.deleteIfExists(path);
    } catch (Exception e) {
        if (file.isFile()) {
            System.out.format("Deletion failed.\nManual deletion of %s needed.\n", file.getAbsolutePath());
return deleted;
voiddeleteDirectory(File path)
delete Directory
try (Stream<Path> walk = Files.walk(path.toPath(), FileVisitOption.FOLLOW_LINKS)) {
    walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
} catch (IOException ignored) {
voiddeleteDirectory(final File directory)
Deletes a directory recursively.
if (!directory.exists()) {
    return;
if (!isSymbolicLink(directory.toPath())) {
    cleanDirectory(directory);
if (!directory.delete()) {
    final String message = "Unable to delete directory " + directory + ".";
...
voiddeleteDirectory(final Path dir, final int maxDepth)
Delete a directory recursively We could use commons-io library for this, if it were using the new java.nio.file API available since Java 7, not the case so far.
Files.walkFileTree(dir, Collections.<FileVisitOption>emptySet(), maxDepth, DELETING_FILE_VISITOR);
booleandeleteDirectory(Path dir)
delete Directory
if (Files.isDirectory(dir)) {
    Path[] children = Files.list(dir).toArray(size -> new Path[size]);
    for (Path p : children) {
        try {
            deleteDirectory(p);
        } catch (DirectoryNotEmptyException e) {
            return false;
System.out.println("removing file or directory : " + dir);
Files.delete(dir);
return true;
voiddeleteDirectory(Path directory)
Deletes a file or directory recursivelly if it exists.
if (Files.exists(directory)) {
    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc == null) {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            } else {
...
voiddeleteDirectory(Path directory)
delete Directory
requireNonNull(directory);
if (!Files.isDirectory(directory))
    throw new IOException(format("%s is not a directory - cannot delete it", directory.toAbsolutePath()));
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
...
voiddeleteDirectory(Path directory)
delete Directory
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
...