Java Utililty Methods Delete File

List of utility methods to do Delete File

Description

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

Method

voiddeleteFile(File file)
delete File
if (!file.exists())
    throw new FileNotFoundException(file.getAbsolutePath());
if (!file.delete()) {
    throw new IOException("Delete operation failed when deleting: " + file);
voiddeleteFile(File file)
Delete the a file or a directory.
if (file.isDirectory()) {
    emptyDirectory(file);
file.delete();
voiddeleteFile(File file)
delete File
if (file != null) {
    for (File childFile : file.listFiles()) {
        if (childFile.isDirectory()) {
            deleteFile(childFile);
        } else {
            if (!childFile.delete()) {
                throw new IOException();
    if (!file.delete()) {
        throw new IOException();
intdeleteFile(File file)
delete File
if (file == null) {
    return -1;
if (file.exists()) {
    if (file.isFile()) {
        if (file.delete()) {
            return 0;
    if (file.isDirectory()) {
        File[] subFiles = file.listFiles();
        if (subFiles != null) {
            for (File subFile : subFiles) {
                deleteFile(subFile);
        if (file.delete()) {
            return 0;
    return -1;
} else {
    return -1;
voiddeleteFile(File file)
delete File
if (file == null || !file.exists()) {
    return;
if (!file.delete()) {
    throw new IOException("Can't delete file : " + file.getPath());
booleandeleteFile(File file)
delete File
if (file == null)
    return false;
return file.delete();
voiddeleteFile(File file)
delete File
if (!file.delete() && file.exists()) {
    throw new IllegalStateException("Could not delete: " + file.getPath());
booleandeleteFile(File file)
Deletes the file provided.
if (!file.exists())
    return false;
File[] files = file.listFiles();
if (files != null) {
    for (int i = 0; i < files.length; i++) {
        File childFile = files[i];
        if (childFile.equals(file))
            continue;
...
booleandeleteFile(File file)
Accommodate Windows bug: If the delete does not work, call System.gc(), wait a little and try again.
if (file.isDirectory()) {
    throw new IOException("File " + file + " isn't a file.");
if (!file.delete()) {
    if (System.getProperty("os.name").equalsIgnoreCase("windows")) {
        file = file.getCanonicalFile();
        System.gc();
    try {
        Thread.sleep(10);
        return file.delete();
    } catch (InterruptedException ex) {
        return file.delete();
return true;
booleandeleteFile(File file)
delete File
if (!file.exists()) {
    return true;
int count = 0;
boolean deleted;
do {
    deleted = file.delete();
    if (!deleted) {
...