Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8295368

File.delete is not working as expected

    XMLWordPrintable

Details

    • windows

    Description

      File.delete() API returns true but in next API call to check check if the file exists, i.e., File.exists(), also returns true even after File.delete() execution has returned.
      This is causing failure to delete the root directory.

      Below is the code implementation:
      -------------------------------------------------
      public static void deleteDirrectoryAndContents(File file) throws IOException
      {
      Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile)
      .forEach(f -> {
      boolean isDeleted = f.delete();
      if (f.exists() && isDeleted) {
      System.out.println("f.exists:" + f.exists());
      System.out.println("isDeleted:" + isDeleted);
      System.out.println("file/directory:" + f.getAbsolutePath());
      }else {
      System.out.print("Deleted Item Count:"+(++deletedFileCount)+"\r");
      }
      });
      }

      In the above code snippet, f.delete() returns true but in the next code statement f.exists() also returns true. When we checked the file system after a few seconds, the file is already deleted. However, the root directory is not deleted as it suspects that the directory is not empty.

      Adding a wait of 20-30 seconds after f.delete() method did the trick and the root directory is deleted as expected from the code.

      This issue appears mostly in 2-core VMs and in a few 4-core VMs. It is not replicable in highly configured systems. We also observed that CPU utilization was showing 100% during the entire process.

      Attachments

        Issue Links

          Activity

            People

              wxiao Weibing Xiao
              shadowbug Shadow Bug
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: