public static void deletePathRecursively ( String path ) throws IOException { if ( ! exists ( path ) ) { return ; } Path root = Paths . get ( path ) ; Files . walkFileTree ( root , 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 e ) throws IOException { if ( e == null ) { Files . delete ( dir ) ; return FileVisitResult . CONTINUE ; } else { throw e ; } } } ) ; }
Deletes a file or a directory recursively if it is a directory .