Java program to delete all the files in a directory recursively (only files)



To delete all the files in a directory recursively (only files), we can use the class Files, which is part of the java.nio.file package. The Files class helps in performing file operations such as creating, deleting, and copying files.

Suppose we have a directory named test with the following structure:

  • file1.txt
  • file2.txt
  • file3.txt
  • subdir1
    • file4.txt
    • file5.txt
  • subdir2
    • file6.txt
    • file7.txt

We want to delete all the files in the test directory and its subdirectories.

Let's look at the steps to delete all the files in a directory recursively:

  • Use the Files.walk() method to get a stream of all files in the directory.
  • Filter the stream to include only files using the Files.isRegularFile() method.
  • Use the Files.delete() method to delete each file.

Example

Following is the Java program to delete all the files in a directory recursively:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class DeleteFiles{
   public static void main(String[] args){
      String dirPath = "D:\test";
        try (Stream<Path> stream = Files.walk(Paths.get(dirPath))){
           stream.filter(Files::isRegularFile)
             .forEach(file -> {
                try {
                   Files.delete(file);
                   System.out.println("Deleted file: " + file);
                } catch (IOException e) {
                   System.err.println("Failed to delete file: " + file + " " + e.getMessage());
                }
             });
        }
        catch (IOException e) {
           System.err.println("Failed to delete files: " + e.getMessage());
        }
   }
}

Output

Following is the output of the above code:

Deleted file: D:\test\file1.txt
Deleted file: D:\test\file2.txt
Deleted file: D:\test\file3.txt
Deleted file: D:\test\subdir1\file4.txt
Deleted file: D:\test\subdir1\file5.txt
Deleted file: D:\test\subdir2\file6.txt
Deleted file: D:\test\subdir2\file7.txt

Now, if we check the test directory, we will see that all the files have been deleted.

Using isFile() method

We can also use the isFile() method of the File class to check if a file is a regular file or not.

    In this method, we will use the listFiles() method of the File class to get all the files and directories in the specified directory.

    Then, we will check if each file is a regular file using the isFile() method.

    Finally, we will delete the file using the delete() method of the File class.

Example

Following is the Java program to delete all the files in a directory recursively using the isFile() method:

import java.io.File;
import java.io.IOException;
public class DeleteFiles{
   public static void main(String[] args){
      String dirPath = "D:\test";
      File dir = new File(dirPath);
      File[] files = dir.listFiles();
      if (files != null) {
         for (File file : files) {
            if (file.isFile()) {
               if (file.delete()) {
                  System.out.println("Deleted file: " + file.getName());
               } else {
                  System.err.println("Failed to delete file: " + file.getName());
               }
            } else if (file.isDirectory()) {
               // Recursively delete files in subdirectory
               deleteFiles(file);
            }
         }
      }
   }
   private static void deleteFiles(File dir) {
      File[] files = dir.listFiles();
      if (files != null) {
         for (File file : files) {
            if (file.isFile()) {
               if (file.delete()) {
                  System.out.println("Deleted file: " + file.getName());
               } else {
                  System.err.println("Failed to delete file: " + file.getName());
               }
            } else if (file.isDirectory()) {
               deleteFiles(file);
            }
         }
      }
   }
}

Output

Following is the output of the above code:

Deleted file: file1.txt
Deleted file: file2.txt
Deleted file: file3.txt
Deleted file: file4.txt
Deleted file: file5.txt
Deleted file: file6.txt
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-09-01T13:56:02+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements