How to delete a whole folder and content?

Beginner picture Beginner · Feb 9, 2011 · Viewed 214.5k times · Source

I want the users of my application to be able to delete the DCIM folder (which is located on the SD card and contains subfolders).

Is this possible, if so how?

Answer

teedyay picture teedyay · Jun 21, 2011

You can delete files and folders recursively like this:

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

    fileOrDirectory.delete();
}