Skip to content
Related Articles

Related Articles

Java Program to Delete a directory

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 02 Nov, 2020

The class named java.io.File represents a file or directory (path names) in the system. This class provides methods to perform various operations on files/directories.

The delete() method of the File class deletes the files and empty directory represented by the current File object. If a directory is not empty or contain files then that cannot be deleted directly. First, empty the directory, then delete the folder.

Suppose there exists a directory with path C:\\GFG. The following image displays the files and directories present inside GFG folder. The subdirectory Ritik contains a file named Logistics.xlsx and subdirectory Rohan contains a file named Payments.xlsx

File Directory

GFG Directory

The following java programs illustrate how to delete a directory.

Method 1: using delete() to delete files and empty folders

  • Provide the path of a directory.
  • Call user-defined method deleteDirectory() to delete all the files and subfolders.

Java




// Java program to delete a directory
  
import java.io.File;
  
class DeleteDirectory {
  
    // function to delete subdirectories and files
    public static void deleteDirectory(File file)
    {
        // store all the paths of files and folders present
        // inside directory
        for (File subfile : file.listFiles()) {
  
            // if it is a subfolder,e.g Rohan and Ritik,
            // recursiley call function to empty subfolder
            if (subfile.isDirectory()) {
                deleteDirectory(subfile);
            }
  
            // delete files and empty subfolders
            subfile.delete();
        }
    }
  
    public static void main(String[] args)
    {
        // store file path
        String filepath = "C:\\GFG";
        File file = new File(filepath);
  
        // call deleteDirectory function to delete
        // subdirectory and files
        deleteDirectory(file);
  
        // delete main GFG folder
        file.delete();
    }
}

Output

Following is the image of C drive where no GFG folder is present.

Deleted File Directory

GFG folder deleted successfully

Method 2: using deleteDirectory() method from commons-io

To use deleteDirectory() method you need to add a commons-io dependency to maven project.

<dependency>

   <groupId>commons-io</groupId>

   <artifactId>commons-io</artifactId>

   <version>2.5</version>

</dependency>

Java




// Java program to delete a directory
  
import java.io.File;
import org.apache.commons.io.FileUtils;
  
class DelteDirectory {
  
    public static void main(String[] args)
    {
        // store file path
        String filepath = "C:\\GFG";
        File file = new File(filepath);
  
        // call deleteDirectory method to delete directory
        // recursively
        FileUtils.deleteDirectory(file);
  
        // delete GFG folder
        file.delete();
    }
}

Output

Following is the image of C drive where no GFG folder is present.

Deleted File Directory

GFG folder deleted Successfully


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!