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

voidforceDelete(String file)
force Delete
forceDelete(new File(file));
voidforceDelete(final File file)
Deletes a file.
if (file == null) {
    return;
if (file.isDirectory()) {
    deleteDirectory(file);
} else {
    final boolean filePresent = file.exists();
    if (!file.delete()) {
...
booleanrecursiveDelete(File fileOrDir)
Recursively delete file or directory
l.debug("Deleting " + fileOrDir + " recursively.");
if (fileOrDir.isDirectory()) {
    for (File innerFile : fileOrDir.listFiles()) {
        if (!recursiveDelete(innerFile)) {
            return false;
l.debug("Deleted" + fileOrDir + " recursively.");
return fileOrDir.delete();
booleanrecursiveDelete(File path)
recursive Delete
if (path.isDirectory()) {
    File[] files = path.listFiles();
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                recursiveDelete(files[i]);
            } else {
                files[i].delete();
...
FileremoveableFile(File folder, String fileName)
removeable File
File file = new File(folder, fileName);
file.deleteOnExit();
return file;
booleanrenameFile(String strSrc, String strDest, boolean deleteIfExist)
Rename file from source to destination
File flSrc = new File(strSrc);
File flDest = new File(strDest);
if (flSrc.getAbsolutePath().equals(flDest.getAbsolutePath()))
    return false;
if (flDest.exists()) {
    if (deleteIfExist)
        flDest.delete();
    else
...
voidsafelyDeleteFile(File filePath)
safely Delete File
if (filePath != null && filePath.exists()) {
    filePath.delete();
voidsafelyDeleteFile(String filePath)
safely Delete File
safelyDeleteFile(new File(filePath));
voiddeleteFolderFile(String filePath, boolean deleteThisPath)
delete Folder File
if (!TextUtils.isEmpty(filePath)) {
    File file = new File(filePath);
    if (file.isDirectory()) {
        File files[] = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            deleteFolderFile(files[i].getAbsolutePath(), true);
    if (deleteThisPath) {
        if (!file.isDirectory()) {
            file.delete();
        } else {
            if (file.listFiles().length == 0) {
                file.delete();
voiddeleteFile(File f)
delete File
if (f.isDirectory()) {
    for (File f2 : f.listFiles())
        deleteFile(f2);
if (!f.delete())
    throw new RuntimeException("Unable to delete file: " + f);