In Java, Files.deleteIfExists(path) deletes the file, empty folder, or link at pathif it exists. It returns a boolean: true when it deleted something, false when there was nothing to delete.
It removes only one worry, the missing path. A folder that is not empty still throws DirectoryNotEmptyException, and permission problems still throw IOExceptions. It is ideal for cleanup code that may run more than once.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.deleteIfExists(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Same argument as delete, plus a result:
Java
boolean removed = Files.deleteIfExists(dir.resolve("cache.dat"));
// true the first time, false afterwards
Call
What it does
Files.deleteIfExists(p)
Deletes if present; true/false.
Files.delete(p)
Throws NoSuchFileException instead of returning false.
if (Files.exists(p)) Files.delete(p)
Racy version: the file can vanish between the two calls.
Since Java 7. The existence check and the delete happen together, so unlike an exists check followed by delete, another program removing the file in between cannot make it throw.
Library declarations
Java
public static boolean deleteIfExists(Path path) throws IOException
Parameters
Parameter
Description
path
The file, empty folder, or link to delete if present.
A failure in the middle. step1.out is created, then creating missing/sub fails because missing does not exist, so step2.out is never made.
Cleanup without bookkeeping. The finally block simply calls deleteIfExists on both names; no flags are needed to remember which files were created.
The results tell the story.true for the file that existed and false for the one that never did; with plain delete, the second call would have thrown inside finally.
Example 3 — Sweep a List of Names, Then Hit a Full Folder
Count how many of several possible files were removed, then show that a non-empty folder still throws.
Java
import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.Path;
public class DeleteIfExistsSweep {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Files.createFile(dir.resolve("a.tmp"));
Files.createFile(dir.resolve("c.tmp"));
String[] names = {"a.tmp", "b.tmp", "c.tmp"};
int removed = 0;
for (String n : names) {
if (Files.deleteIfExists(dir.resolve(n))) {
removed++;
}
}
System.out.println("Removed " + removed + " of " + names.length);
Path box = Files.createDirectory(dir.resolve("box"));
Path item = Files.createFile(box.resolve("item.txt"));
try {
Files.deleteIfExists(box);
} catch (DirectoryNotEmptyException e) {
System.out.println("box: DirectoryNotEmptyException");
}
Files.deleteIfExists(item);
Files.deleteIfExists(box);
Files.delete(dir);
}
}
Output
Run with java DeleteIfExistsSweep. The output is the same on Windows, macOS, and Linux.
Removed 2 of 3
box: DirectoryNotEmptyException
How It Works
Some present, some not. Only a.tmp and c.tmp exist; b.tmp is skipped with a false result, and the counter ends at 2.
Exists is not enough. box exists, but it holds item.txt, so deleteIfExists throws DirectoryNotEmptyException just like delete would.
Empty it first. Deleting item.txt makes box empty, and the next call removes it.
Frequently Asked Questions
true if the file or folder existed and was deleted, false if nothing existed at that path.
Yes. It still throws DirectoryNotEmptyException for a non-empty folder and IOException for other problems, such as missing permissions or a file locked by another program on Windows.
Yes. exists() followed by delete() can fail if another program removes the file in between. deleteIfExists does both in one step.
No. The folder must be empty. Delete its contents first, for example by walking the tree in reverse order.
Use delete when a missing file means a bug and you want an exception. Use deleteIfExists for cleanup code where the file may or may not be there.