Delete folder from internal storage in android?

duggu picture duggu · Nov 16, 2012 · Viewed 31.8k times · Source

How to delete folder(contain some folder and file) from internal storage? Folder have some below tree.

folder
|_________  C2 (folder)
             |________1 (folder)
                      |________1.gif
                      |________2.gif
                      |________3.gif
                      |________4.gif
             |________2 (folder)
                      |________1.gif
                      |________2.gif
                      |________3.gif
                      |________4.gif
             |________3 (folder)
                      |________1.gif
                      |________2.gif
                      |________3.gif
                      |________4.gif
             |________4 (folder)
                      |________1.gif
                      |________2.gif
                      |________3.gif
                      |________4.gif
             |________5 (folder)
                      |________1.gif
                      |________2.gif
                      |________3.gif
                      |________4.gif

|_________  C2.xml (file)

I want to delete folder and containing all files

fil.delete();

System.out.println("boolean =>" + fil.delete());

but above code shows false. Please help.

Answer

Kalai.G picture Kalai.G · Nov 16, 2012

Check this out.

public void deleteRecursive(File fileOrDirectory) {

   if (fileOrDirectory.isDirectory()) {
       for (File child : fileOrDirectory.listFiles()) {
          deleteRecursive(child);
       }
   }

   fileOrDirectory.delete();
 }

for explaination How to delete a whole folder and content?