How to delete non empty directory in java

In this post, we will see how to delete Directory/Folder which is non empty. You can use java.io.File 's delete empy folder but you can not delete it if it is non empty.
There are multiple ways to do it.
  • Using java recursion
  • Using Apache common IO

Using java recursion:

It is very straight forward to do it. It will iterate through all files in the folder. If it is a file then we can directly delete it but if It finds folder inside it then we again call same method.
// Delete using recursion
 public static void delete(File file)
   throws IOException{

  if(file.isDirectory()){

   //We can directly delete if we found empty directory
   if(file.list().length==0){

    file.delete();
    System.out.println("Deleting folder : " 
      + file.getAbsolutePath());

   }else{

    //list all the files in directory
    File files[] = file.listFiles();

    for (File temp : files) {
     //recursive delete
     delete(temp);
    }

    // Check directory again, if we find it empty, delete it
    if(file.list().length==0){
     file.delete();
     System.out.println("Deleting folder : " 
       + file.getAbsolutePath());
    }
   }

  }else{
   //if file, then we can directly delete it
   file.delete();
   System.out.println("Deleting file  : " + file.getAbsolutePath());
  }
 }


Using Apache common IO:

Delete non empty folder is very straight forward. You just need to call FileUtils.deleteDirectory() method.
// Delete using Apache common IO
 public static void deleteUsingApacheIO(File f)
 {
  try {
   FileUtils.deleteDirectory(f);
  } catch (IOException e) {

   e.printStackTrace();
  }
 }

Java Program:

package org.arpit.java2blog;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class DeleteNonEmptyDirectoryMain {

 public static void main(String[] args) {
  
  
  System.out.println("Deleting using recursion");
  System.out.println("---------------");
  File folder= new File("/Users/Arpit/Desktop/Blog/temp1");
  // Using recusrion
  try {
   delete(folder);
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  System.out.println("---------------");
  System.out.println("Deleting using Apache IO");
  File folder2= new File("/Users/Arpit/Desktop/Blog/temp2");
  deleteUsingApacheIO(folder2);
  System.out.println("Deleting folder : " 
    + folder2.getAbsolutePath());
 }

 // Delete using recursion
 public static void delete(File file)
   throws IOException{

  if(file.isDirectory()){

   //We can directly delete if we found empty directory
   if(file.list().length==0){

    file.delete();
    System.out.println("Deleting folder : " 
      + file.getAbsolutePath());

   }else{

    //list all the files in directory
    File files[] = file.listFiles();

    for (File temp : files) {
     //recursive delete
     delete(temp);
    }

    // Check directory again, if we find it empty, delete it
    if(file.list().length==0){
     file.delete();
     System.out.println("Deleting folder : " 
       + file.getAbsolutePath());
    }
   }

  }else{
   //if file, then we can directly delete it
   file.delete();
   System.out.println("Deleting file  : " + file.getAbsolutePath());
  }
 }

 // Delete using Apache common IO
 public static void deleteUsingApacheIO(File f)
 {
  try {
   FileUtils.deleteDirectory(f);
  } catch (IOException e) {

   e.printStackTrace();
  }
 }
}

When I ran above program, I got following output
Deleting using recursion
---------------
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/.DS_Store
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/BinaryTree/.DS_Store
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/BinaryTree/verticalSum.jpg.png
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/BinaryTree/VerticalSumOrder.png
Deleting folder : /Users/Arpit/Desktop/Blog/temp1/BinaryTree
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/jersey json/.DS_Store
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyJsonCountry.png
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyJsonCountryID.png
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyMavenBuild.png
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyMavenCleanInstall.png
Deleting file  : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyProjectStructure.png
Deleting folder : /Users/Arpit/Desktop/Blog/temp1/jersey json
Deleting folder : /Users/Arpit/Desktop/Blog/temp1
---------------
Deleting using Apache IO
Deleting folder : /Users/Arpit/Desktop/Blog/temp2



Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012