158

I want to create and delete a directory using Java, but it isn't working.

File index = new File("/home/Work/Indexer1");
if (!index.exists()) {
    index.mkdir();
} else {
    index.delete();
    if (!index.exists()) {
        index.mkdir();
    }
}
spongebob
  • 8,370
  • 15
  • 50
  • 83
Mr.G
  • 3,413
  • 2
  • 16
  • 20

30 Answers30

254

Just a one-liner.

import org.apache.commons.io.FileUtils;

FileUtils.deleteDirectory(new File(destination));

Documentation here

JohnK
  • 6,865
  • 8
  • 49
  • 75
Barry Knapp
  • 2,771
  • 2
  • 14
  • 9
  • updated link: https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html – OhadR Feb 07 '17 at 15:21
  • 36
    um...no. This is a one-liner with an external dependency, which is an important thing to keep in mind. The only time using an external dependency like this is this simple is when you're doing a personal home project, or your company really doesn't care about the possibility of getting sued. – searchengine27 Apr 05 '17 at 21:34
  • 25
    @searchengine27 but it seems the library is under Apache Commons so the risk of getting sued is negligible https://www.whitesourcesoftware.com/whitesource-blog/top-10-apache-license-questions-answered/. – simtim Jul 23 '17 at 12:29
  • 3
    @simtim you're missing the point entirely. A company will never approve a library to use without a team of lawyers pouring over the terms of use and end user agreements, and any other legal documents associated with the library first. Somebody has to pay those lawyers...sometimes nobody wants to, which means the developer doesn't get to use it. The bigger the company you work for, the more red tape you have to go through. – searchengine27 Jul 24 '17 at 22:02
  • 59
    @searchengine27 no, you're missing the point entirely. A company that needs an army of lawyers to allow to use apache commons is the absolute pathology, and nothing near the norm in IT world. I've never heard of anyone having such problems and if you have such problems you have most likely access to SO blocked so the answer would not be accessible to you anyway. – 9ilsdx 9rvj 0lo Mar 20 '18 at 16:20
  • @9ilsdx9rvj0lo This isn't the "IT" world, this is corporate software development. Have you worked in the corporate world yet? You clearly have not because you're making wild connections between different things that have no correlation. If you had any experience in the corporate world, you would know what corporate espionage was, and how terms of service or rights of use violations can cost a company millions of dollars. And then you would know why a team of lawyers needs to evaluate a tool before a developer goes anywhere near it. – searchengine27 Mar 20 '18 at 17:32
  • @9ilsdx9rvj0lo There's a big difference between writing software to use in-house and writing software for a product that is being distributed (sold, leased, licensed, etc.) In the latter case, there are significantly more legal hoops to jump through, though they shouldn't be prohibitive. But they can make the difference between using a simple solution for a small task and pulling in a new 3rd-party package. – Jeff Learman Apr 02 '18 at 23:37
  • 1
    Java API lacks such a basic feature and because of this, we have to use an external library – nomadSK25 Apr 25 '20 at 15:35
  • 1
    @searchengine27 I wonder how these gigantic legally-paranoid companies get anything done in a world where just using some simple Java package (e.g. Google PubSub) requires 60 dependencies. Sure the legal team may find that Google's PubSub license is okay, but then there are 60 open-source dependencies, each with their own license. And each of those 60 dependencies has transitive dependencies on 5-10 other packages. It's even worse in the Node NPM JavaScript world, where the simplest project using React has probably over 1000 dependent packages, each with their own license. – deltamind106 Oct 24 '22 at 18:17
  • 3
    @searchengine27 I have worked for large enterprise companies and can confirm that they do allow you to use open source libraries, it's just a fact of modern software development. However at my former employer they did keep long lists of all the dependencies including transitive dependencies and their licenses. if the license was not on a standard list (e.g. EPL, MPL, Apache etc) then we had to get specific approval for it. but it certainly didn't require an army of lawyers – Adam Burley Dec 08 '22 at 18:42
127

Java isn't able to delete folders with data in it. You have to delete all files before deleting the folder.

Use something like:

String[]entries = index.list();
for(String s: entries){
    File currentFile = new File(index.getPath(),s);
    currentFile.delete();
}

Then you should be able to delete the folder by using index.delete() Untested!

Cemron
  • 1,443
  • 1
  • 12
  • 15
  • 47
    This won't delete non-empty subdirectories. – spongebob Feb 15 '15 at 17:20
  • 5
    Be VERY careful. If index is a symbolic link to another directory, you'll wind up deleting the contents of the other directory. Unfortunately, I've not yet found a good way to detect symbolic links on Windows in Java 6, though Java 7 provides Files.isSymbolicLink(). – Hank Schultz Nov 11 '15 at 16:04
  • 1
    Solution: wrap this chunk of code in `if (!index.delete()) {...}`. Then, if index is a symbolic link, it's deleted regardless of whether it appears it has contents. – Hank Schultz Nov 11 '15 at 16:16
  • This will throw a NullPointerException if there is an I/O exception while reading the directory. The code should check whether `entries` is null. – mernst Oct 31 '17 at 23:55
  • The method delete files only if `index` is a path of directory, example `my/path/`, otherwise throw NullPointerException. i.e. if `index` is `my/path/file.txt` has an error – ferpaxecosanxez May 06 '22 at 10:46
110

This works, and while it looks inefficient to skip the directory test, it's not: the test happens right away in listFiles().

void deleteDir(File file) {
    File[] contents = file.listFiles();
    if (contents != null) {
        for (File f : contents) {
            deleteDir(f);
        }
    }
    file.delete();
}

Update, to avoid following symbolic links:

void deleteDir(File file) {
    File[] contents = file.listFiles();
    if (contents != null) {
        for (File f : contents) {
            if (! Files.isSymbolicLink(f.toPath())) {
                deleteDir(f);
            }
        }
    }
    file.delete();
}
Jeff Learman
  • 2,914
  • 1
  • 22
  • 31
  • 2
    As it turns out, there is a bug in this. If another process deletes the files during the loop, it can cause an exception that should be caught and ignored. – Jeff Learman Sep 08 '17 at 21:53
  • 4
    @9ilsdx9rvj0lo Rather than being snarky, perhaps you could provide an edit to handle symlinks. The OP said *nothing* about symbolic links in his post. Just creating and deleting a directory. Please also list out the "many things missing". Help us out. – Perry Tew Mar 21 '18 at 17:31
  • @PerryTew I'm not being snarky. I'm just pointing out, that I do fully disagree with your comment about the answer being better because no external libraries are being used. It is not. There is a good reason people are using apache commons : you don't have to program any single thing yourself. Symlinks are just an example of things you'll miss writing everything from scratch. – 9ilsdx 9rvj 0lo Mar 22 '18 at 07:31
  • 3
    It's not a matter of better/worse, but pros and cons. Not relying on external libraries is sometimes a significant benefit. Of course, there's a significant benefit to using tried-and-true software. It's up to the developer to balance the issues. If there are bugs other than the two already mentioned, we'd certainly like to know about them. – Jeff Learman Apr 02 '18 at 23:40
65

I prefer this solution on java 8:

  Files.walk(pathToBeDeleted)
    .sorted(Comparator.reverseOrder())
    .map(Path::toFile)
    .forEach(File::delete);

From this site: http://www.baeldung.com/java-delete-directory

Update, to close the stream:

  try (Stream<Path> pathStream = Files.walk(repoDirFile.toPath())) {
    pathStream.sorted(Comparator.reverseOrder())
      .map(Path::toFile)
      .forEach(File::delete);
  }
mernst
  • 7,437
  • 30
  • 45
nirmal
  • 1,842
  • 18
  • 11
  • 9
    Note that this may have scalability issues since it builds the full list, creates a sorted copy, and then iterates over the sorted copy. Back in the bad old days when memory wasn't inexhaustible, this would be a very bad idea. It's concise but at a cost in space (O(N) vs O(1)) and efficiency (O(N log N) vs O(N)). This wouldn't matter in most use cases. – Jeff Learman Apr 02 '18 at 22:59
  • I should have said "space O(N) vs O(depth)" above, where depth is the depth of the directory tree (comparing this solution to recursive ones.) – Jeff Learman Apr 02 '18 at 23:28
  • 2
    this is elegant, works and does not rely on external libraries. loved it – Leo Feb 12 '19 at 18:48
  • 5
    Doesn't this have the problem of file handle leaks? This example does _not_ close the stream returned by `Files.walk()`, which is explicitly indicated in the API docs. I know that if you don't close the stream returned by `Files.list()` for example, you can run out of handles and the program will crash. See e.g. https://stackoverflow.com/q/36990053/421049 and https://stackoverflow.com/q/26997240/421049 . – Garret Wilson Apr 18 '19 at 21:38
  • You don't call any terminal function in that stream, for this reason no files will actually be deleted. If you want that the stream will actually iterate files and delete them you will have to call terminal function like .count() on it. .close() is not a terminal function. – RenatoIvancic May 08 '21 at 05:32
  • @GarretWilson + posterity, I tested this using the methods described in your link (by querying for open handles after running this on a loop) it shows that this does not leave any open handles at all, the terminal operation `forEach` must be closing the stream. – egerardus May 12 '21 at 03:51
  • 1
    Why do you need to sort if `walk()` is depth-first? – shmosel Jun 23 '21 at 18:29
  • 2
    @shmosel Because forEach will start deleting from the beginning and will not be able to delete the directory with the contents. Therefore, the tree must be turned in the opposite direction and deleted from the end. – Alexey Bril Aug 12 '21 at 20:40
  • There's no need to map from `Path` to `File`. Just use `Files::delete` do delete the `Path` directly. – Rangi Keen Sep 20 '22 at 15:17
  • Explanation why do we need to sort: "Here, Files.walk() returns a Stream of Path that we sort in reverse order. This places the paths denoting the contents of directories before directories itself. Thereafter it maps Path to File and deletes each File." – chill appreciator Jun 15 '23 at 07:53
28

In JDK 7 you could use Files.walkFileTree() and Files.deleteIfExists() to delete a tree of files. (Sample: http://fahdshariff.blogspot.ru/2011/08/java-7-deleting-directory-by-walking.html)

In JDK 6 one possible way is to use FileUtils.deleteQuietly from Apache Commons which will remove a file, a directory, or a directory with files and sub-directories.

Fabian Streitel
  • 2,702
  • 26
  • 36
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
26

Using Apache Commons-IO, it is following one-liner:

import org.apache.commons.io.FileUtils;

FileUtils.forceDelete(new File(destination));

This is (slightly) more performant than FileUtils.deleteDirectory.

JRA_TLL
  • 1,186
  • 12
  • 23
17

As mentioned, Java is unable to delete a folder containing files, so first delete the files and then the folder.

Here's a simple example to do this:

import org.apache.commons.io.FileUtils;



// First, remove files from into the folder 
FileUtils.cleanDirectory(folder/path);

// Then, remove the folder
FileUtils.deleteDirectory(folder/path);

Or:

FileUtils.forceDelete(new File(destination));
Gavriel Cohen
  • 4,355
  • 34
  • 39
13

One more choice is to use Spring's org.springframework.util.FileSystemUtils relevant method which will recursively delete all content of the directory.

File directoryToDelete = new File(<your_directory_path_to_delete>);
FileSystemUtils.deleteRecursively(directoryToDelete);

That will do the job!

dZ.
  • 404
  • 1
  • 5
  • 9
9

My basic recursive version, working with older versions of JDK:

public static void deleteFile(File element) {
    if (element.isDirectory()) {
        for (File sub : element.listFiles()) {
            deleteFile(sub);
        }
    }
    element.delete();
}
spongebob
  • 8,370
  • 15
  • 50
  • 83
Pierre Lemée
  • 342
  • 3
  • 2
  • 2
    This will throw a NullPointerException if there is an I/O exception while reading the directory. The code should check whether `listFiles()` returns null, rather than calling `isDirectory()`. – mernst Oct 31 '17 at 23:57
9

This is the best solution for Java 7+:

public static void deleteDirectory(String directoryFilePath) throws IOException
{
    Path directory = Paths.get(directoryFilePath);

    if (Files.exists(directory))
    {
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>()
        {
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException
            {
                Files.delete(path);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path directory, IOException ioException) throws IOException
            {
                Files.delete(directory);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
7

Guava 21+ to the rescue. Use only if there are no symlinks pointing out of the directory to delete.

com.google.common.io.MoreFiles.deleteRecursively(
      file.toPath(),
      RecursiveDeleteOption.ALLOW_INSECURE
) ;

(This question is well-indexed by Google, so other people usig Guava might be happy to find this answer, even if it is redundant with other answers elsewhere.)

Laurent Caillette
  • 1,281
  • 2
  • 14
  • 20
5

You can try this

public static void deleteDir(File dirFile) {
    if (dirFile.isDirectory()) {
        File[] dirs = dirFile.listFiles();
        for (File dir: dirs) {
            deleteDir(dir);
        }
    }
    dirFile.delete();
}
IMXQD
  • 231
  • 2
  • 11
4

I like this solution the most. It does not use 3rd party library, instead it uses NIO2 of Java 7.

/**
 * Deletes Folder with all of its content
 *
 * @param folder path to folder which should be deleted
 */
public static void deleteFolderAndItsContent(final Path folder) throws IOException {
    Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc != null) {
                throw exc;
            }
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    });
}
Javo
  • 435
  • 1
  • 5
  • 16
3

2020 here :)

With Apache commons io FileUtils, contrary to the "pure" Java variants a folder does not need to be empty to be deleted. To give you a better overview I list the variants here, the following 3 may throw exceptions for various reasons:

  • cleanDirectory: Cleans a directory without deleting it
  • forceDelete: Deletes a file. If file is a directory, delete it and all sub-directories
  • forceDeleteOnExit: Schedules a file to be deleted when JVM exits. If file is directory delete it and all sub-directories. Not recommended for running servers as JVM may not exit any time soon ...

The following variant never throws exceptions (even if the file is null !)

  • deleteQuietly: Deletes a file, never throwing an exception. If it is a directory, delete it and all sub-directories.

One more thing to know is dealing with symbolic links, it will delete the symbolic link and not the target folder... be careful.

Also keep in mind that deleting a large file or folder can be a blocking operation for a good while ... so if you do not mind having it run async do it (in a background thread via executor for example).

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
2

In this

index.delete();

            if (!index.exists())
               {
                   index.mkdir();
               }

you are calling

 if (!index.exists())
                   {
                       index.mkdir();
                   }

after

index.delete();

This means that you are creating the file again after deleting File.delete() returns a boolean value.So if you want to check then do System.out.println(index.delete()); if you get true then this means that file is deleted

File index = new File("/home/Work/Indexer1");
    if (!index.exists())
       {
             index.mkdir();
       }
    else{
            System.out.println(index.delete());//If you get true then file is deleted




            if (!index.exists())
               {
                   index.mkdir();// here you are creating again after deleting the file
               }




        }

from the comments given below,the updated answer is like this

File f=new File("full_path");//full path like c:/home/ri
    if(f.exists())
    {
        f.delete();
    }
    else
    {
        try {
            //f.createNewFile();//this will create a file
            f.mkdir();//this create a folder
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
Community
  • 1
  • 1
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
2

If you have subfolders, you will find troubles with the Cemron answers. so you should create a method that works like this:

private void deleteTempFile(File tempFile) {
        try
        {
            if(tempFile.isDirectory()){
               File[] entries = tempFile.listFiles();
               for(File currentFile: entries){
                   deleteTempFile(currentFile);
               }
               tempFile.delete();
            }else{
               tempFile.delete();
            }
        getLogger().info("DELETED Temporal File: " + tempFile.getPath());
        }
        catch(Throwable t)
        {
            getLogger().error("Could not DELETE file: " + tempFile.getPath(), t);
        }
    }
Panthro
  • 3,247
  • 2
  • 32
  • 37
2

You can use FileUtils.deleteDirectory. JAVA can't delete the non-empty foldres with File.delete().

Issam Ressani
  • 203
  • 1
  • 4
  • 7
1

directry cannot simply delete if it has the files so you may need to delete the files inside first and then directory

public class DeleteFileFolder {

public DeleteFileFolder(String path) {

    File file = new File(path);
    if(file.exists())
    {
        do{
            delete(file);
        }while(file.exists());
    }else
    {
        System.out.println("File or Folder not found : "+path);
    }

}
private void delete(File file)
{
    if(file.isDirectory())
    {
        String fileList[] = file.list();
        if(fileList.length == 0)
        {
            System.out.println("Deleting Directory : "+file.getPath());
            file.delete();
        }else
        {
            int size = fileList.length;
            for(int i = 0 ; i < size ; i++)
            {
                String fileName = fileList[i];
                System.out.println("File path : "+file.getPath()+" and name :"+fileName);
                String fullPath = file.getPath()+"/"+fileName;
                File fileOrFolder = new File(fullPath);
                System.out.println("Full Path :"+fileOrFolder.getPath());
                delete(fileOrFolder);
            }
        }
    }else
    {
        System.out.println("Deleting file : "+file.getPath());
        file.delete();
    }
}
Indranil.Bharambe
  • 1,462
  • 3
  • 14
  • 25
1

we can use the spring-core dependency;

boolean result = FileSystemUtils.deleteRecursively(file);
Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
1

Most of answers (even recent) referencing JDK classes rely on File.delete() but that is a flawed API as the operation may fail silently.
The java.io.File.delete() method documentation states :

Note that the java.nio.file.Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.

As replacement, you should favor Files.delete(Path p) that throws an IOException with a error message.

The actual code could be written such as :

Path index = Paths.get("/home/Work/Indexer1");

if (!Files.exists(index)) {
    index = Files.createDirectories(index);
} else {

    Files.walk(index)
         .sorted(Comparator.reverseOrder())  // as the file tree is traversed depth-first and that deleted dirs have to be empty  
         .forEach(t -> {
             try {
                 Files.delete(t);
             } catch (IOException e) {
                 // LOG the exception and potentially stop the processing

             }
         });
    if (!Files.exists(index)) {
        index = Files.createDirectories(index);
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

Here is a simple way to do it :

public void deleteDirectory(String directoryPath)  {
      new Thread(new Runnable() {
          public void run() {
             for(String e: new File(directoryPath).list()) {
                 if(new File(e).isDirectory()) 
                     deleteDirectory(e);
                 else 
                     new File(e).delete();
             }
          }
      }).start();
  }
Xoma Devs
  • 35
  • 1
  • 7
1
You can simply remove the directory which contains a single file or multiple files using the apache commons library.

gradle import :

implementation group: 'commons-io', name: 'commons-io', version: '2.5'

File file=new File("/Users/devil/Documents/DummyProject/hello.txt");
    
 File parentDirLocation=new File(file.getParent);
    
//Confirming the file parent is a directory or not.
             
if(parentDirLocation.isDirectory){
    
    
 //after this line the mentioned directory will deleted.
 FileUtils.deleteDirectory(parentDirLocation);
   
 }
Santh
  • 2,221
  • 1
  • 5
  • 7
0
private void deleteFileOrFolder(File file){
    try {
        for (File f : file.listFiles()) {
            f.delete();
            deleteFileOrFolder(f);
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}
0
        import org.apache.commons.io.FileUtils;

        List<String> directory =  new ArrayList(); 
        directory.add("test-output"); 
        directory.add("Reports/executions"); 
        directory.add("Reports/index.html"); 
        directory.add("Reports/report.properties"); 
        for(int count = 0 ; count < directory.size() ; count ++)
        {
        String destination = directory.get(count);
        deleteDirectory(destination);
        }





      public void deleteDirectory(String path) {

        File file  = new File(path);
        if(file.isDirectory()){
             System.out.println("Deleting Directory :" + path);
            try {
                FileUtils.deleteDirectory(new File(path)); //deletes the whole folder
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else {
        System.out.println("Deleting File :" + path);
            //it is a simple file. Proceed for deletion
            file.delete();
        }

    }

Works like a Charm . For both folder and files . Salam :)

0

You can make recursive call if sub directories exists

import java.io.File;

class DeleteDir {
public static void main(String args[]) {
deleteDirectory(new File(args[0]));
}

static public boolean deleteDirectory(File path) {
if( path.exists() ) {
  File[] files = path.listFiles();
  for(int i=0; i<files.length; i++) {
     if(files[i].isDirectory()) {
       deleteDirectory(files[i]);
     }
     else {
       files[i].delete();
     }
  }
}
return( path.delete() );
}
}
prem30488
  • 2,828
  • 2
  • 25
  • 57
0

You may also use this to delete a folder that contains subfolders and files.

  1. Fist, create a recursive function.

     private void recursiveDelete(File file){
    
             if(file.list().length > 0){
                 String[] list = file.list();
                 for(String is: list){
                     File currentFile = new File(file.getPath(),is);
                     if(currentFile.isDirectory()){
                             recursiveDelete(currentFile);
                     }else{
                         currentFile.delete();
                     }
                 }
             }else {
                 file.delete();
             }
         }
    
  2. then, from your initial function use a while loop to call the recursive.

     private boolean deleteFolderContainingSubFoldersAndFiles(){
    
             boolean deleted = false;
             File folderToDelete = new File("C:/mainFolderDirectoryHere");
    
             while(folderToDelete != null && folderToDelete.isDirectory()){
                 recursiveDelete(folderToDelete);
             }
    
             return deleted;
         }
    
0
List<File> temp = Arrays.asList(new File("./DIRECTORY").listFiles());
for (int i = 0; i < temp.size(); i++)
{
    temp.get(i).delete();
}
Izzy Kiefer
  • 189
  • 1
  • 4
-1

Some of these answers seem unnecessarily long:

if (directory.exists()) {
    for (File file : directory.listFiles()) {
        file.delete();
    }
    directory.delete();
}

Works for sub directories too.

Adam Short
  • 498
  • 7
  • 28
-2

Remove it from else part

File index = new File("/home/Work/Indexer1");
if (!index.exists())
{
     index.mkdir();
     System.out.println("Dir Not present. Creating new one!");
}
index.delete();
System.out.println("File deleted successfully");
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
-3

You can use this function

public void delete()    
{   
    File f = new File("E://implementation1/");
    File[] files = f.listFiles();
    for (File file : files) {
        file.delete();
    }
}
Aatish Sai
  • 1,647
  • 1
  • 26
  • 41
Piyush Rumao
  • 363
  • 4
  • 7
  • It works fine with a directory with all closed files. But when tried on directory with open files it doesnt work. Can you help me find a way to delete folder inspite of open files – Piyush Rumao Mar 22 '16 at 05:30
  • 2
    This won't delete non-empty subdirectories. – Pang May 14 '16 at 04:39