After Directory.Delete the Directory.Exists returning true sometimes?

Directory.Delete calls the Windows API function RemoveDirectory. The observed behavior is documented:

The RemoveDirectory function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed.

The .NET documentation is unfortunately missing this information. Whether the static Directory.Delete method opens a handle to the directory is not documented. Likewise, if it does, it is not documented when the handle is closed.

Without any of this information, the best you can do is to poll for completion:

Directory.Delete(tempFolder, true);
while (Directory.Exists(tempFolder)) Thread.Sleep(0);
// At this point the directory has been removed from the filesystem

Even though polling should generally be avoided in preference of events, installing a filesystem watcher would be a bit over the top for this. Still, keep in mind, that this operation does not come for free, particularly when dealing with a network drive.


Update: With .NET's Reference Source available, the implementation of Directory.Delete can be inspected. The first action of this method is to iterate over all files and delete them. The iteration is implemented using FindFirstFile/FindNextFile. The returned handle is stored as a SafeFindHandle, a concrete subclass of SafeHandle. As the documentation points out, the native handle is freed through a concrete ReleaseHandle override. ReleaseHandle is called from a (postponed) critical finalizer. Since finalization is non-deterministic, this explains the open handle, responsible for the delayed directory delete.

This information, however, does not help in finding a better solution than the one described above (polling for completion).


Other answers to this question did not identify the core issue, and work by coincidence at best. BanksySan's answer adds unrelated code that introduces a delay to allow time for open handles to be closed. Byeni's answer is closer, yet still off: When he talks about the object referencing the directory he almost nails it. However, the object referencing the directory is called a handle, a native resource. Native resources are disposed of in finalizers, and GC.Collect() does not run finalizers. This, too, appears to work by buying extra time.

Use DirectoryInfo instead, and call Refresh() on that.

        var dir = new DirectoryInfo(tempFolder);
        dir.Delete();
        dir.Refresh();

Because we are performing many operations on the directory, it is more performant to use DirectoryInfo rather that Directory. This probably explains why there is no Refresh() on the static class, it is meant for one off operations and so would never need to be refreshed.

If might be worth adding a Thread.Sleep(0) after the refresh to relinquish the thread and put it to the back of the pool. Haven't tested that though, it's just a musing.

Tags:

C#

.Net

Io