Delete Directory Recursively

Recursively delete a directory using Java NIO.

Javadoc available at https://www.javatapas.com/docs/javatapas/io/DeleteDirectoryRecursively.html


public static void deleteDirectoryRecursively(Path dirToDeleteName) throws IOException {

	Files.walkFileTree(dirToDeleteName, 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;
		}
	});

}