If you try to delete a not empty folder, a java.nio.file.DirectoryNotEmptyException will be thrown. This tutorial will show 4 differents ways to delete folder recursively in Java :
The code source is available on Github. Download the source
Table of contents
1- Native Java
JDK 1.0+
To delete a not empty folder, you must first delete its children. Below a recursive Java code to delete a given folder :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/** * Deleting recursively a directory with native Java. * @author Radouane ROUFID. * */ public class DeleteDirectoryNativeJavaTutorial { public static void main(String[] args) { // Directory path. String directoryPath = "C:/dev/directory"; File file = new File(directoryPath); try { //Deleting the directory recursively. delete(file); System.out.println("Directory has been deleted recursively !"); } catch (IOException e) { System.out.println("Problem occurs when deleting the directory : " + directoryPath); e.printStackTrace(); } } /** * Delete a file or a directory and its children. * @param file The directory to delete. * @throws IOException Exception when problem occurs during deleting the directory. */ private static void delete(File file) throws IOException { for (File childFile : file.listFiles()) { if (childFile.isDirectory()) { delete(childFile); } else { if (!childFile.delete()) { throw new IOException(); } } } if (!file.delete()) { throw new IOException(); } } } |
2- Walking the folder using WalkFileTree
JDK 1.7+
The java.nio.file.Files class provides walkFileTree to navigate in the folder. Below a code using it to delete recursively a given folder :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; /** * Deleting recursively a directory with native Java. * https://docs.oracle.com/javase/tutorial/essential/io/walk.html * * @author Radouane ROUFID. * */ public class DeleteDirectoryWalkFileTutorial { public static void main(String[] args) { // Directory path. String directoryPath = "C:/dev/directory"; try { // Deleting the directory recursively. delete(directoryPath); System.out.println("Directory has been deleted recursively !"); } catch (IOException e) { System.out.println("Problem occurs when deleting the directory : " + directoryPath); e.printStackTrace(); } } /** * Delete a file or a directory and its children. * * @param file * The directory to delete. * @throws IOException * Exception when problem occurs during deleting the directory. */ private static void delete(String directoryName) throws IOException { Path directory = Paths.get(directoryName); 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; } }); } } |
In this example, I used the SimpleFileVisitor class to walk the file. You can create your own “file visitor class” by implemeting use The FileVisitor interface.
3- Using Apache commons IO
Since JDK 1.6 For Commons IO 2.4
Since JDK 1.5 For Commons IO 2.2
Appache Commons IO provides FileUtils to handle files and folders and delete them recursively. Below how to use it :
-
Download the dependency
If you are working on a Maven project, add the following to your pom.xml
1 2 3 4 5 6 7 |
<dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> |
If not, download the jar and add it to your classpath.
-
Delete folder recursively using FileUtils.deleteDirectory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * Delete a directory recursively. * @author Radouane ROUFID. * */ public class DeleteDirectoryWithFileUtilsTutorials { public static void main(String[] args) { // Directory path. String directoryPath = "C:/dev/directory"; File file = new File(directoryPath); try { //Deleting the directory recursively using FileUtils. FileUtils.deleteDirectory(file); System.out.println("Directory has been deleted recursively !"); } catch (IOException e) { System.out.println("Problem occurs when deleting the directory : " + directoryPath); e.printStackTrace(); } } } |
4- Using SpringUtils
SpringUtil provides FileSystemUtils to copy and delete recursively folders.
-
Download the dependency
If you are working on a Maven project, add the following to your pom.xml
1 2 3 4 5 6 7 |
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.5.RELEASE</version> </dependency> </dependencies> |
If not, download the jar and add it to your classpath.
-
Delete folder recursively using FileSystemUtils.deleteRecursively
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.File; import org.springframework.util.FileSystemUtils; /** * Delete directory with Spring Util. * @author Radouane ROUFID. * */ public class DeleteDirectorySpringUtils { public static void main(String[] args) { /// Directory path. String directoryPath = "C:/dev/directory"; File file = new File(directoryPath); // Deleting the directory recursively with SpringUtil. if(!FileSystemUtils.deleteRecursively(file)) { System.out.println("Problem occurs when deleting the directory : " + directoryPath); } } } |
The code source is available on Github. Download the source