How to delete all files and folders in one folder on Android
Check this link also Delete folder from internal storage in android?.
void deleteRecursive(File fileOrDirectory) { if (fileOrDirectory.isDirectory()) for (File child : fileOrDirectory.listFiles()) deleteRecursive(child); fileOrDirectory.delete();}
Simplest way would be to use FileUtils.deleteDirectory from the Apache Commons IO library.
File dir = new File("root path");FileUtils.deleteDirectory(dir);
Bear in mind this will also delete the containing directory.
Add this line in gradle file to have Apache
compile 'org.apache.commons:commons-io:1.3.2'
File file = new File("C:\\A\\B"); String[] myFiles; myFiles = file.list(); for (int i=0; i<myFiles.length; i++) { File myFile = new File(file, myFiles[i]); myFile.delete(); } B.delete();// deleting directory.
You can write method like this way :Deletes all files and subdirectories under dir.Returns true if all deletions were successful.If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // The directory is now empty so delete it return dir.delete();}