Deleting a File or Directory in Java

Learn to delete a specified file or directory in Java. Note that different methods behave differently for deleting non-empty directories.

1. Deleting with File Class

To delete a file, File class provides the following methods:

1.1. boolean delete()

  • It deletes the specified file or directory. In the case of a directory, the directory must be empty in order to be deleted.
  • This method returns true if and only if the file or directory is successfully deleted; false otherwise.
  • In case of any permission issues, SecurityException is thrown.
  • In the file cannot be deleted for any reason then it does not throw any exception, rather it simply returns false.

1.2. void deleteOnExit()

  • It registers the file for deletion when the virtual machine terminates.
  • It is useful in the case of unit testing to delete temporary files after the test execution is finished.
  • Note that once deletion has been requested, it is not possible to cancel the request.
  • Deletion will be attempted only when the JVM terminates normally, otherwise, the behavior is unspecified.
  • If a file or directory is already for deletion then this method has no effect.
//Deleting a file immidiately
File file = new File("c:/temp/one.txt");
boolean deleted  = file.delete();

//Registering for deletion
File file = new File("c:/temp/two.txt");
file.deleteOnExit();

2. Deleting with java.nio.file.Files

The Files class also provides two following methods:

2.1. void delete(path)

  • Similar to File.delete(), this method also deletes a file or an empty directory.
  • The difference is that this method throws IOException if the file cannot be deleted which is useful in debugging the reason for failure.
  • It throws NoSuchFileException if the specified file or directory does not exist.
  • Similarly, it throws DirectoryNotEmptyException if the specified directory is not empty.

2.2. boolean deleteIfExists(path)

  • This method is a little different version of delete(). It does not throw NoSuchFileException if the file or directory is not present.
  • It deletes a file or directory if it exists.
  • This method returns true if the file was deleted by this method; false if the file could not be deleted.
Path path = Path.of("c:/temp/one.txt");
Files.delete(path);

//or 

Path path = Path.of("c:/temp/two.txt");
boolean success = Files.deleteIfExists(path);

3. Deleting with Commons IO’s FileUtils

The FileUtils class has following useful methods for deleting the files and directories:

File delete(file) : deletes a file or directory. Internally it uses Files.delete() method.
void deleteDirectory(file) : deletes a directory recursively. It returns IOException in case the deletion is unsuccessful.
boolean deleteQuietly(file) : deletes a file without ever throwing an exception. If the file is a directory, delete it and all sub-directories. It does not require the directory to be empty as it is needed with other methods.

FileUtils.delete(file);

FileUtils.deleteQuietly(new File("c:/temp"));

boolean success = FileUtils.deleteDirectory(new File("c:/temp"));

4. Conclusion

Deleting a file or directory in Java is a very simple operation and mostly done in a single statement. Still, it may fail sometimes for two reasons i.e. permission issues and a non-empty directory.

As a best practice, we can use Files.delete(path) for deleting a file and FileUtils.deleteDirectory() for deleting a directory recursively.

Happy Learning !!

Source Code on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.