0

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

2 Answers2

0

Try to use:

   FileUtils.deleteDirectory(File directory);

For more info go to link1 and link2

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

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.

Luke Melaia
  • 1,470
  • 14
  • 22