Search code examples
javafile-ioapache-commons-io

Delete all files in directory (but not directory) - one liner solution


I want to delete all files inside ABC directory.

When I tried with FileUtils.deleteDirectory(new File("C:/test/ABC/")); it also deletes folder ABC.

Is there a one liner solution where I can delete files inside directory but not directory?


Solution

  • import org.apache.commons.io.FileUtils;
    
    FileUtils.cleanDirectory(directory); 
    

    There is this method available in the same file. This will also recursively deletes all sub-folders and files under them.

    Docs: org.apache.commons.io.FileUtils.cleanDirectory