← Back to Core Java Course | Chapter 12: File I/O | Lesson 4 of 9

Java Delete Files

A file can be removed from disk using either the classic File class's delete method, which returns a boolean, or the newer NIO Files.delete method, which throws a specific exception on failure.
Syntax
java
File file = new File("file_name");
file.delete();

Deleting a File with the File Class

A file can be removed from disk using the File class's delete method, which attempts to delete the file at that path and returns a boolean indicating whether the deletion actually succeeded.

Example: Deleting a File with the File Class

java
// Import java.io.File so it can be used by its short name
import java.io.File;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new File object and store it in file
		File file = new File("temp.txt");
		file.createNewFile();
		// Declare deleted and set it to file.delete()
		boolean deleted = file.delete();
		// Print a line to the console
		System.out.println(deleted);
	}
}

Checking Deletion Success

The delete method never throws an exception on failure -- it silently returns false whether the file didn't exist in the first place or the deletion failed for some other reason like a permissions issue.

Example: Checking Deletion Success

java
import java.io.File;
public class Main {
	public static void main(String[] args) {
		File file = new File("does-not-exist.txt");
		boolean deleted = file.delete(); // false, no exception thrown
		System.out.println(deleted);
	}
}

Deleting Directories

The same delete method also removes empty directories, but it will fail and return false if the directory still contains any files or subdirectories, since it does not delete recursively.

Example: Deleting Directories

java
import java.io.File;
public class Main {
	public static void main(String[] args) {
		File dir = new File("empty-folder");
		dir.mkdir();
		System.out.println(dir.delete()); // succeeds: directory is empty
	}
}

Deleting Files with NIO

The NIO Files utility class offers delete, which throws a specific exception on failure, and deleteIfExists, which safely returns false instead of throwing when the target file simply isn't there.

Example: Deleting Files with NIO

java
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		Path path = Path.of("temp.txt");
		Files.createFile(path);
		Files.delete(path); // throws on failure
		System.out.println(Files.deleteIfExists(path)); // false: already gone
	}
}

Handling Deletion Errors

Handling delete failures well means checking the boolean or exception the API provides rather than assuming success, since a failed deletion often points to the file being open elsewhere or a lack of file system permissions.

Example: Handling Deletion Errors

java
// Import java.io.File so it can be used by its short name
import java.io.File;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new File object and store it in file
		File file = new File("temp.txt");
		// Check whether !file.delete()
		if (!file.delete()) {
			// Print a line to the console
			System.out.println("Deletion failed: check permissions or existence");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Ignoring the boolean returned by File.delete(), so a failure goes unnoticed.
  2. Trying to delete a non-empty directory, which fails.
  3. Using a wrong path, which reports that the file was not found.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.