Java 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

voiddoDelete(File file)
do Delete
if (!file.delete()) {
    throw new IOException("Unable to delete: " + file);
voiddoDelete(File path)
do Delete
if (!path.exists())
    return;
if (path.isDirectory()) {
    for (File child : path.listFiles()) {
        doDelete(child);
if (!path.delete()) {
...
voiddoDeleteEmptyDir(String dir)
do Delete Empty Dir
boolean success = (new File(dir)).delete();
if (success) {
    System.out.println("Successfully deleted empty directory: " + dir);
} else {
    System.out.println("Failed to delete empty directory: " + dir);
booleandoDeleteEmptyParentQuietly(File parent)
do Delete Empty Parent Quietly
try {
    if (parent == null) {
        return true;
    boolean result = true;
    File parentFile = parent.getParentFile();
    if (parent.list().length == 0) {
        parent.delete();
...
booleandoDeleteFileOrDir(File fileOrDir)
Deletes specified file or directory.
if (fileOrDir.isDirectory() && !doEmptyDir(fileOrDir)) {
    return false;
return fileOrDir.delete();
booleandoDeleteQuietly(File file)
do Delete Quietly
try {
    boolean result = true;
    if (file.isDirectory()) {
        for (File aFile : file.listFiles()) {
            boolean res = doDeleteQuietly(aFile);
            if (result) {
                result = res;
    file.delete();
    return result;
} catch (Throwable ignore) {
    return false;
voidfileDelete(String fileName)
Deletes a file.
File file = new File(fileName);
file.delete();
booleanfileDelete(String path)
Deletes a file at the specified path location.
if (isPathValid(path)) {
    File file = new File(path);
    if (fileExist(path)) {
        return file.delete();
    } else {
        return true;
} else
...
intfileDelete(String path)
file Delete
File file = new File(path);
if (!file.exists() || file.isDirectory())
    return 4;
if (file.delete()) {
    return 0;
return 1;