title: Deleting non-empty directories in Java
body: <p>Supposing I have a <code>File f</code> that represents a directory, then <code>f.delete()</code> will only delete the directory if it is empty.  I've found a <a href="http://www.exampledepot.com/egs/java.io/DeleteDir.html" rel="noreferrer">couple</a> of <a href="http://www.rgagnon.com/javadetails/java-0483.html" rel="noreferrer">examples</a> online that use <code>File.listFiles()</code> or <code>File.list()</code> to get all the files in the directory and then recursively traverses the directory structure and delete all the files.  However, since it's possible to create infinitely recursive directory structures (in both <a href="http://blogs.msdn.com/oldnewthing/archive/2004/12/27/332704.aspx" rel="noreferrer">Windows</a> and Linux (with symbolic links)) presumably it's possible that programs written in this style might never terminate.</p>
  
  <p>So, is there a better way to write such a program so that it doesn't fall into these pitfalls?  Do I need to keep track of everywhere I've traversed and make sure I don't go around in circles or is there a nicer way?</p>
  
  <p><strong>Update:</strong> In response to some of the answers (thanks guys!) - I'd rather the code didn't follow symbolic links and stayed within the directory it was supposed to delete.  Can I rely on the Commons-IO implementation to do that, even in the Windows case?</p>