title: How to delete files of a directory but not the folders
body: <p>I have create some code that deletes all the files in a folder, the issue is while this is great, I want to be able to delete all the files in a directory but leave the folders intact, so I do not have to go into each folder and delete the files in it, this is my current code :</p>
  
  <pre><code>@ViewScoped
  @ManagedBean
  public class Delete {
  
      public void DeleteFiles() throws IOException {
          System.out.println("Called deleteFiles");
          File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/up617648/");
  
          String[] myFiles;
          if (file.isDirectory()) {
              myFiles = file.list();
              for (int i = 0; i &lt; myFiles.length; i++) {
                  File myFile = new File(file, myFiles[i]);
                  System.out.println(myFile);
                  myFile.delete();
              }
              DeleteFiles2();
          }
  
      }
      public void DeleteFiles2() throws IOException {
          System.out.println("Called deleteFiles2");
          File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/up617648/");
  
          String[] myFiles;
          if (file.isDirectory()) {
              myFiles = file.list();
              for (int i = 0; i &lt; myFiles.length; i++) {
                  File myFile = new File(file, myFiles[i]);
                  System.out.println(myFile);
                  myFile.delete();
              }
          }
  
      }
  }
  </code></pre>
  
  <p>How could I modify my code to achieve this aim?</p>
  
  <p>EDIT :</p>
  
  <p>I have changed my code to :</p>
  
  <pre><code>@ViewScoped
  @ManagedBean
  public class Delete {
  
      public void DeleteFiles() throws IOException {
          System.out.println("Called deleteFiles");
          File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
  
          String[] myFiles;
          if (file.isDirectory()) {
              myFiles = file.list();
              for (int i = 0; i &lt; myFiles.length; i++) {
                  File myFile = new File(file, myFiles[i]);
                  System.out.println(myFile);
                 if (!myFile.isDirectory()) {
                      myFile.delete();
                  }
              }
              DeleteFiles2();
          }
  
      }
  </code></pre>
  
  <p>And while this does call all the folders, this is my console:</p>
  
  <pre><code>INFO: Called deleteFiles
  INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\cam01342
  INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\geg00061
  INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\null
  INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\up617648
  INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\up626088
  INFO: Called deleteFiles2
  </code></pre>
  
  <p>Tt does not delete the files inside these folders, how can I do this?</p>
  
  <p>EDIT 3: Using recursion I am now getting a stack overflow error.</p>
  
  <p>Here's my code :</p>
  
  <pre><code>public void DeleteFiles() {
      File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
      System.out.println("Called deleteFiles");
      if (file.isDirectory()) {
          for (File f : file.listFiles()) {
              DeleteFiles();
          }
      } else {
          file.delete();
      }
  }
  </code></pre>