codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

46 / 65 topics
43Java Files44Java Create & Write Files45Java Read Files46Java Delete Files47Java I/O Streams48FileInputStream & FileOutputStream49BufferedReader & BufferedWriter
Tutorials/Java Programming/Java Delete Files
☕Java Programming

Java Delete Files

Updated 2026-05-12
15 min read

Java Delete Files

In this tutorial, you'll learn how to delete files and folders in Java. Understanding how to manage file deletion is crucial for maintaining the integrity of your application's data storage. Whether you need to clean up temporary files or remove obsolete data, knowing how to delete files and directories efficiently is an essential skill.

Introduction

Deleting files and folders in Java can be accomplished using the File class from the java.io package. This tutorial will guide you through deleting both individual files and entire directories. You'll learn about best practices, common pitfalls, and how to handle exceptions that may arise during these operations.

Deleting a File

Basic File Deletion

To delete a file in Java, you can use the delete() method of the File class. This method returns true if the file is successfully deleted, otherwise it returns false.

DeleteFile.java
1import java.io.File;
2
3public class DeleteFile {
4 public static void main(String[] args) {
5 File file = new File("example.txt");
6
7 if (file.delete()) {
8 System.out.println(file.getName() + " deleted successfully.");
9 }; else {
10 System.out.println("Failed to delete the file.");
11 }
12 }
13}
Output
example.txt deleted successfully.

Handling Exceptions

While the delete() method does not throw an exception, it's important to handle potential issues such as file permissions or non-existent files. You can use the exists() method to check if the file exists before attempting to delete it.

DeleteFileWithCheck.java
1import java.io.File;
2
3public class DeleteFileWithCheck {
4 public static void main(String[] args) {
5 File file = new File("example.txt");
6
7 if (file.exists()) {
8 if (file.delete()) {
9 System.out.println(file.getName() + " deleted successfully.");
10 } else {
11 System.out.println("Failed to delete the file.");
12 }
13 } else {
14 System.out.println("File does not exist.");
15 }
16 }
17}
Output
example.txt deleted successfully.

Deleting a Folder

Basic Directory Deletion

To delete an empty directory, you can use the delete() method in the same way as for files. However, if the directory contains files or other directories, you must first delete its contents recursively.

DeleteEmptyDirectory.java
1import java.io.File;
2
3public class DeleteEmptyDirectory {
4 public static void main(String[] args) {
5 File dir = new File("emptyDir");
6
7 if (dir.delete()) {
8 System.out.println(dir.getName() + " deleted successfully.");
9 } else {
10 System.out.println("Failed to delete the directory.");
11 }
12 }
13}
Output
emptyDir deleted successfully.

Recursive Directory Deletion

To delete a non-empty directory, you need to recursively delete all its contents. Here's how you can do it:

DeleteNonEmptyDirectory.java
1import java.io.File;
2
3public class DeleteNonEmptyDirectory {
4 public static void main(String[] args) {
5 File dir = new File("nonEmptyDir");
6
7 if (deleteDirectory(dir)) {
8 System.out.println(dir.getName() + " deleted successfully.");
9 } else {
10 System.out.println("Failed to delete the directory.");
11 }
12 }
13
14 public static boolean deleteDirectory(File dir) {
15 if (dir.isDirectory()) {
16 File[] files = dir.listFiles();
17 for (File file : files) {
18 if (!deleteDirectory(file)) {
19 return false;
20 }
21 }
22 }
23 return dir.delete();
24 }
25}
Output
nonEmptyDir deleted successfully.

Practical Example

Let's create a practical example that deletes both a file and a directory. The program will first delete a file named "data.txt" and then attempt to delete a directory named "backup".

FileAndDirectoryDeletion.java
1import java.io.File;
2
3public class FileAndDirectoryDeletion {
4 public static void main(String[] args) {
5 // Delete a file
6 File file = new File("data.txt");
7 if (file.exists()) {
8 if (file.delete()) {
9 System.out.println(file.getName() + " deleted successfully.");
10 } else {
11 System.out.println("Failed to delete the file.");
12 }
13 } else {
14 System.out.println("File does not exist.");
15 }
16
17 // Delete a directory
18 File dir = new File("backup");
19 if (dir.exists()) {
20 if (deleteDirectory(dir)) {
21 System.out.println(dir.getName() + " deleted successfully.");
22 } else {
23 System.out.println("Failed to delete the directory.");
24 }
25 } else {
26 System.out.println("Directory does not exist.");
27 }
28 }
29
30 public static boolean deleteDirectory(File dir) {
31 if (dir.isDirectory()) {
32 File[] files = dir.listFiles();
33 for (File file : files) {
34 if (!deleteDirectory(file)) {
35 return false;
36 }
37 }
38 }
39 return dir.delete();
40 }
41}
Output
data.txt deleted successfully.
backup deleted successfully.

Summary

  • Use the delete() method of the File class to delete files and empty directories.
  • Always check if a file or directory exists before attempting to delete it using the exists() method.
  • For non-empty directories, use a recursive approach to delete all contents before deleting the directory itself.

What's Next?

In the next tutorial, we'll explore Java I/O Streams. Understanding how to read from and write to streams is essential for handling more complex file operations and data processing tasks. Stay tuned!


PreviousJava Read FilesNext Java I/O Streams

Recommended Gear

Java Read FilesJava I/O Streams