Android Utililty Methods File Delete

List of utility methods to do File Delete

Description

The list of methods to do File Delete are organized into topic(s).

Method

booleandeleteFile(String strSrc)
Delete file
File flSrc = new File(strSrc);
return flSrc.delete();
voiddeleteFileOnly(String path)
delete File Only
File file = new File(path);
if (file.exists() && file.isFile()) {
    file.delete();
booleandeleteFiles(File file)
delete Files
return deleteFiles(file, ".*");
booleandeleteFiles(File file, String regex)
recursively delete all sub files which matches regular expression
if (!file.exists())
    return true;
if (file.isDirectory()) {
    File[] subFiles = file.listFiles();
    for (File subFile : subFiles) {
        if (!deleteFiles(subFile, regex))
            return false;
} else if (file.getName().matches(regex))
    return file.delete();
return true;
voiddeleteFilesRecursive(File src)
delete all the files in a directory recursively
if (!src.exists()) {
    if (!src.mkdirs()) {
        throw new IOException(
                "deleteFiles: Could not create direcotry: "
                        + src.getAbsolutePath() + ".");
} else if (!src.canRead()) { 
    throw new IOException("copyFiles: No right to source: "
...
booleandeleteFolder(File targetFolder)
delete Folder
try {
    File[] childFile = targetFolder.listFiles();
    if (childFile == null) {
        return !targetFolder.exists();
    int size = childFile.length;
    if (size > 0) {
        for (int i = 0; i < size; i++) {
...
voiddeleteFolders(File dir)
delete Folders
if (dir.isDirectory()) {
    File[] children = dir.listFiles();
    for (File child : children) {
        deleteFolders(child);
    dir.delete();
voiddeleteIfExists(File file)
Calls #delete delete (file) if file != null and file exists.
if ((file != null) && file.exists())
    delete(file);
voiddeleteOldFile(String strPath, String strWildcard, int iOffset)
Delete unused file
booleandeleteRecursively(File dir)
Deletes a file recursively.
if (dir == null) {
    return false;
boolean result = true;
if (dir.isDirectory()) {
    for (File child : dir.listFiles()) {
        result = result && deleteRecursively(child);
return result && dir.delete();