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

Fahim Parkar picture Fahim Parkar · Nov 2, 2012 · Viewed 246.9k times · Source

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?

Answer

Reddy picture Reddy · Nov 2, 2012
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