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

booleandeleteDirectory(Path directory)
Deletes a directory recursively
if (directory != null) {
    try {
        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 {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
        });
    } catch (IOException ignored) {
        return false;
return true;
voiddeleteDirectory(Path dirToDelete)
delete Directory
if (!Files.isDirectory(dirToDelete))
    throw new IllegalArgumentException("the path is not a directory");
Files.walkFileTree(dirToDelete, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        System.out.println("Deleting file: " + file);
        Files.delete(file);
        return CONTINUE;
...
voiddeleteDirectory(Path path)
delete Directory
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
...
voiddeleteDirectory(String path)
delete Directory
File file = new File(path);
File[] contents = file.listFiles();
if (contents != null) {
    for (File f : contents) {
        f.delete();
file.delete();
...
voiddeleteDirectoryAndContents(String dir)
delete Directory And Contents
deleteDirectoryAndContents(Paths.get(dir));
voiddeleteDirectoryRecursively(Path dir)
Delete a directory recursively.
if (!Files.exists(dir)) {
    return;
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
...
voiddeleteDirectoryRecursively(Path path)
delete Directory Recursively
Files.walkFileTree(path, new FileVisitor<Path>() {
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        Files.delete(dir);
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
...
voiddeleteDirectorySelectively(Path path, Predicate predicate)
delete Directory Selectively
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
        if (predicate.test(file)) {
            Files.delete(file);
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult postVisitDirectory(Path directory, IOException ex) throws IOException {
        if (ex != null) {
            return FileVisitResult.TERMINATE;
        if (predicate.test(directory)) {
            Files.delete(directory);
        return FileVisitResult.CONTINUE;
});
voiddeleteDirIfEmpty(Path dir)
delete Dir If Empty
if (Files.isDirectory(dir)) {
    try {
        if (!Files.list(dir).findFirst().isPresent())
            Files.deleteIfExists(dir);
    } catch (DirectoryNotEmptyException | NoSuchFileException ignored) {
voiddeleteDirIfExists(Path dirPath)
delete Dir If Exists
if (Files.exists(dirPath)) {
    deleteDir(dirPath);