How to delete directory in Java

To delete a directory, you can simply use the File.delete(), but the directory must be empty in order to delete it.

Often times, you may require to perform recursive delete in a directory, which means all it’s sub-directories and files should be delete as well, see below example :

Directory recursive delete example

Delete the directory named “C:\\mkyong-new“, and all it’s sub-directories and files as well. The code is self-explanatory and well documented, it should be easy to understand.


package com.mkyong.file;
 
import java.io.File;
import java.io.IOException;
 
public class DeleteDirectoryExample
{
    private static final String SRC_FOLDER = "C:\\mkyong-new";
	
    public static void main(String[] args)
    {	
    	
    	File directory = new File(SRC_FOLDER);
 
    	//make sure directory exists
    	if(!directory.exists()){
 
           System.out.println("Directory does not exist.");
           System.exit(0);
 
        }else{
 
           try{
        	   
               delete(directory);
        	
           }catch(IOException e){
               e.printStackTrace();
               System.exit(0);
           }
        }
 
    	System.out.println("Done");
    }
 
    public static void delete(File file)
    	throws IOException{
 
    	if(file.isDirectory()){
 
    		//directory is empty, then delete it
    		if(file.list().length==0){
    			
    		   file.delete();
    		   System.out.println("Directory is deleted : " 
                                                 + file.getAbsolutePath());
    			
    		}else{
    			
    		   //list all the directory contents
        	   String files[] = file.list();
     
        	   for (String temp : files) {
        	      //construct the file structure
        	      File fileDelete = new File(file, temp);
        		 
        	      //recursive delete
        	     delete(fileDelete);
        	   }
        		
        	   //check the directory again, if empty then delete it
        	   if(file.list().length==0){
           	     file.delete();
        	     System.out.println("Directory is deleted : " 
                                                  + file.getAbsolutePath());
        	   }
    		}
    		
    	}else{
    		//if file, then delete it
    		file.delete();
    		System.out.println("File is deleted : " + file.getAbsolutePath());
    	}
    }
}

Result


File is deleted : C:\mkyong-new\404.php
File is deleted : C:\mkyong-new\archive.php
...
Directory is deleted : C:\mkyong-new\includes
File is deleted : C:\mkyong-new\index.php
File is deleted : C:\mkyong-new\index.php.hacked
File is deleted : C:\mkyong-new\js\hoverIntent.js
File is deleted : C:\mkyong-new\js\jquery-1.4.2.min.js
File is deleted : C:\mkyong-new\js\jquery.bgiframe.min.js
Directory is deleted : C:\mkyong-new\js\superfish-1.4.8\css
Directory is deleted : C:\mkyong-new\js\superfish-1.4.8\images
Directory is deleted : C:\mkyong-new\js\superfish-1.4.8
File is deleted : C:\mkyong-new\js\superfish-navbar.css
...
Directory is deleted : C:\mkyong-new
Done

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
13 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
11 Comment authors
komal shivhareromeoKhaled ElgendybharathNaeem Recent comment authors
newest oldest most voted
bharath
Guest
bharath

it wont delete if sub-folder has sub-folder and subfiles

komal shivhare
Guest
komal shivhare

Helpful..Thank you so much…!!

romeo
Guest
romeo

Eres Otro nivel. gracias!!

Khaled Elgendy
Guest
Khaled Elgendy

public static void deleteFiles(File d){ for(File f1: d.listFiles()){ if(f1.isDirectory()){ deleteFiles(f1); } f1.delete(); } d.delete(); }

Naeem
Guest
Naeem

Thank you so much!

a
Guest
a

Thanks! Quick and easy

tisaik
Guest
tisaik

Thank you a lot!
I use it on my project.

Mansour
Guest
Mansour

Hi,

More Simple is to use FileUtils of Apache Commons (org.apache.commons.io.FileUtils):

deleteDirectory

public static void deleteDirectory(File directory)
throws IOException
Deletes a directory recursively.
Parameters:
directory – directory to delete
Throws:
IOException – in case deletion is unsuccessful

    FileUtils.deleteDirectory(f);

Best regards.

test
Guest
test

import java.io.File; public class removerDir { public static void main(String args[]) { File file = new File("C:\\Users\\486834\\Desktop\\df"); recursiveDelete(file); } public static void recursiveDelete(File file) { if (!file.exists()) return; if (file.isDirectory()) { for (File f : file.listFiles()) { recursiveDelete(f); } } file.delete(); } }

asdf
Guest
asdf

Awesome Simplified code above

do
Guest
do

even simpler: void recursiveDelete(File file) { if (!file.exists()) return; if (file.isDirectory()) { for (File f : file.listFiles()) { recursiveDelete(f); } } file.delete(); }

da
Guest
da

more simple: private static void recursiveDelete(File file) { if (!file.exists()) return; if (file.isDirectory()) { for (File f : file.listFiles()) recursiveDelete(f); file.delete(); } else { file.delete(); } } public static void deleteDir(String dirname) { File directory = new File(dirname); recursiveDelete(directory); }

da
Guest
da

private static void recursiveDelete(File file) { if (!file.exists()) return; if (file.isDirectory()) { for (File f : file.listFiles()) recursiveDelete(f); file.delete(); } else { file.delete(); } } public static void DeleteDir(String dirname) { File directory = new File(dirname); recursiveDelete(directory); }

trackback
Java I/O Tutorials

[…] Delete directory […]