title: C++/Win32: How to wait for a pending delete to complete
body: <p><strong>Solved:</strong></p>
  
  <ul>
  <li>Workable solution: <a href="https://stackoverflow.com/questions/3764072/c-win32-how-to-wait-for-a-pending-delete-to-complete/3764298#3764298">sbi's answer</a></li>
  <li>Explanation for what really happens: <a href="https://stackoverflow.com/questions/3764072/c-win32-how-to-wait-for-a-pending-delete-to-complete/3764322#3764322">Hans's answer</a></li>
  <li>Explanation for why OpenFile doesn't pass through "DELETE PENDING": <a href="https://stackoverflow.com/questions/3764072/c-win32-how-to-wait-for-a-pending-delete-to-complete/3776438#3776438">Benjamin's answer</a></li>
  </ul>
  
  <p><strong>The Problem:</strong></p>
  
  <p>Our software is in large part an interpreter engine for a proprietary scripting language. That scripting language has the ability to create a file, process it, and then delete the file. These are all separate operations, and no file handles are kept open in between  these operations.</p>
  
  <p>(I.e. during the file creation, a handle is created, used for writing, then closed. During the file processing portion, a separate file handle opens the file, reads from it, and is closed at EOF. And <em>finally</em>, delete uses ::DeleteFile which only has use of a filename, not a file handle at all).</p>
  
  <p>Recently we've come to realize that a particular macro (script) fails sometimes to be able to create the file at some random subsequent time (i.e. it succeeds during the first hundred iterations of "create, process, delete", but when it comes back to creating it a hundred and first time, Windows replies "Access Denied").</p>
  
  <p>Looking deeper into the issue, I have written a very simple program that loops over something like this:</p>
  
  <pre><code>while (true) {
      HANDLE hFile = CreateFileA(pszFilename, FILE_ALL_ACCESS, FILE_SHARE_READ,
                                 NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
      if (hFile == INVALID_HANDLE_VALUE)
          return OpenFailed;
  
      const DWORD dwWrite = strlen(pszFilename);
      DWORD dwWritten;
  
      if (!WriteFile(hFile, pszFilename, dwWrite, &amp;dwWritten, NULL) || dwWritten != dwWrite)
          return WriteFailed;
  
      if (!CloseHandle(hFile))
          return CloseFailed;
  
      if (!DeleteFileA(pszFilename))
          return DeleteFailed;
  }
  </code></pre>
  
  <p>As you can see, this is direct to the Win32 API and is pretty darn simple. I create a file, write to it, close the handle, delete it, rinse, repeat...</p>
  
  <p>But somewhere along the line, I'll get an Access Denied (5) error during the CreateFile() call. Looking at sysinternal's ProcessMonitor, I can see that the underlying issue is that there is a pending delete on the file while I'm trying to create it again.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li>Is there a way to wait for the delete to complete?</li>
  <li>Is there a way to detect that a file is pending deletion?</li>
  </ul>
  
  <p>We have tried the first option, by simply WaitForSingleObject() on the HFILE. But the HFILE is always closed before the WaitForSingleObject executes, and so WaitForSingleObject always returns WAIT_FAILED.  Clearly, trying to wait for the closed handle doesn't work.</p>
  
  <p>I could wait on a change notification for the folder that the file exists in. However, that seems like an extremely overhead-intensive kludge to what is a problem only occasionally (to wit: in my tests on my Windows&nbsp;7 x64 E6600 PC it typically fails on iteration 12000+ -- on other machines, it can happen as soon as iteration 7 or 15 or 56 or never).</p>
  
  <p>I have been unable to discern any CreateFile() arguments that would explicitly allow for this ether. No matter what arguments CreateFile has, it really is not okay with opening a file for <em>any</em> access when the file is pending deletion.</p>
  
  <p>And since I can see this behavior on both an Windows&nbsp;XP box and on an x64 Windows&nbsp;7 box, I am quite certain that this is core NTFS behavior "as intended" by Microsoft.  So I need a solution that allows the OS to complete the delete before I attempt to proceed, preferably without tying up CPU cycles needlessly, and without the extreme overhead of watching the folder that this file is in (if possible).</p>
  
  <p><a href="https://stackoverflow.com/questions/3764072/c-win32-how-to-wait-for-a-pending-delete-to-complete/3764298#3764298">1</a> Yes, this loop returns on a failure to write or a failure to close which leaks, but since this is a simple console test application, the application itself exits, and Windows guarantees that all handles are closed by the OS when a process completes. So no leaks exist here.</p>
  
  <pre><code>bool DeleteFileNowA(const char * pszFilename)
  {
      // Determine the path in which to store the temp filename
      char szPath[MAX_PATH];
      strcpy(szPath, pszFilename);
      PathRemoveFileSpecA(szPath);
  
      // Generate a guaranteed to be unique temporary filename to house the pending delete
      char szTempName[MAX_PATH];
      if (!GetTempFileNameA(szPath, ".xX", 0, szTempName))
          return false;
  
      // Move the real file to the dummy filename
      if (!MoveFileExA(pszFilename, szTempName, MOVEFILE_REPLACE_EXISTING))
          return false;
  
      // Queue the deletion (the OS will delete it when all handles (ours or other processes) close)
      if (!DeleteFileA(szTempName))
          return false;
  
      return true;
  }
  </code></pre>