How to delete a whole folder and content? How to delete a whole folder and content? android android

How to delete a whole folder and content?


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();}


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

Updated as per comments

File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here"); if (dir.isDirectory()) {    String[] children = dir.list();    for (int i = 0; i < children.length; i++)    {       new File(dir, children[i]).delete();    }}


We can use the command line arguments to delete a whole folder and its contents.

public static void deleteFiles(String path) {    File file = new File(path);    if (file.exists()) {        String deleteCmd = "rm -r " + path;        Runtime runtime = Runtime.getRuntime();        try {            runtime.exec(deleteCmd);        } catch (IOException e) { }    }}

Example usage of the above code:

deleteFiles("/sdcard/uploads/");