code: public static void deleteFiles(File dir, FilenameFilter filter) {
          // validations
          if(dir == null) {
              throw new DataUtilException("The delete directory parameter can not be a null value");
          } else if(!dir.exists() || !dir.isDirectory()) {
              throw new DataUtilException("The delete directory does not exist: " + dir.getAbsolutePath());
          }
  
          // delete files
          File [] files;
          if(filter == null) {
              files = dir.listFiles();
          } else {
              files = dir.listFiles(filter);
          }
          if (files != null) {
              for(File file : files) {
                  if(file.isFile()) {
                      if(!file.delete()) {
                          throw new DataUtilException("Failed to delete file: " + file.getAbsolutePath());
                      }
                  }
              }
          }
      }
docs: Delete files in a directory matching a filename filter
  @param dir directory
  @param filter filename filter, if null then delete all files