Delete folder from internal storage in android? Delete folder from internal storage in android? android android

Delete folder from internal storage in android?


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?


You can't delete root folder if sub folder contains any files. So for that you have to first delete each of sub files and then you will able to remove the folder.

Your code is valid, I just update like:

 boolean deleted = mypath.delete();

ie. mypath is your File Directory.


Let me tell you first thing you cannot delete the Rootfolder because it is a system folder. As you delete it manually on phone it will delete the contents of that folder, but not the Root folder. You can delete its contents by using the method below:

private void DeleteRecursive(File fileOrDirectory) {    if (fileOrDirectory.isDirectory())        for (File child : fileOrDirectory.listFiles())        {            child.delete();            DeleteRecursive(child);        }    fileOrDirectory.delete();}