Android examples for java.io:Folder
delete folder recursively and check Delete_Max_Recursion_Depth
import java.io.File; import android.util.Log; public class Main { /**// w w w .jav a 2s . co m * The maximum recursion depth we allow when deleting directories */ private static final int DELETE_MAX_RECURSION_DEPTH = 1; private static final String TAG = ""; /** * Delete a file/directory * * @param fileToDelete * the file/directory to be deleted * @param recursive * if a directory needs to be deleted with all of it's content, set * this to true. please note, that recursion is currently limited to a * depth of 1 subfolder * @return true if the file/directory was completely deleted, false otherwise */ public static boolean delete(File fileToDelete, boolean recursive) { return delete(fileToDelete, recursive, 0); } /** * Delete a file/directory * * @param fileToDelete * the file/directory to be deleted * @param recursive * if the deletion should be recursive * @param recursionDepth * takes care of the depth of recursion and aborts deletion if * DELETE_MAX_RECURSION_DEPTH is reached * @return */ private static boolean delete(File fileToDelete, boolean recursive, int recursionDepth) { // if we're deeper than one recursion/subfolder, we'll cancel deletion if (recursionDepth > DELETE_MAX_RECURSION_DEPTH) { Log.w(TAG, "DELETE_MAX_RECURSION_DEPTH (" + DELETE_MAX_RECURSION_DEPTH + ") reached. Directory deletion aborted."); return false; } boolean deleted = false; // If it's a directory and we should delete it recursively, try to delete all // childs if (fileToDelete.isDirectory() && recursive) { for (File child : fileToDelete.listFiles()) { if (!delete(child, true, recursionDepth + 1)) { Log.w(TAG, "deletion of [" + child + "] failed, aborting now..."); return false; } } } deleted = fileToDelete.delete(); boolean isDir = fileToDelete.isDirectory(); if (deleted) { Log.i(TAG, "deleted " + (isDir ? "directory" : "file") + " [" + fileToDelete + "]"); } else { Log.w(TAG, "unable to delete " + (isDir ? "directory" : "file") + " [" + fileToDelete + "]"); } return deleted; } }