I have the need to delete a folder/directory or content regardless if you have not, only for JAVA
folder.delete()
that does not work.
I been looking, but they are outdated methods
folder.delete()
will only delete the folder if it is empty.
If you want to delete an entire folder tree you will need to walk it, delete all the files within it and then delete the folder itself.
You can use Files.walkFileTree
to accomplish this:
public static void deleteEntireFolder(File folder) throws IOException {
if (!folder.isDirectory()) {
folder.delete();
return;
}
Files.walkFileTree(Paths.get(
folder.getAbsolutePath()),//The folder path
EnumSet.of(FileVisitOption.FOLLOW_LINKS),//Do you want to go through shortcuts?
100,//How many folders deep you want to walk.
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
file.toFile().delete();
return FileVisitResult.CONTINUE;
}
});
}
You will probably need to refine this method, but as it stands it should get the job done.