13
using System.IO;
Directory.Delete("someFolder",true);
Directory.Create("someFolder");

Will the third line be executed after the dir was deleted or while the directory is being deleted? Do I have to put the first command into a "Task" and wait until it is finished?

5
  • 2
    C# is synchronous by nature. Maybe you're a bit confused by al the new stuff about asynchrony in .NET. And also: I would imagine the reason for the downvotes being the fact that you formulated your question as a comment in the code example.
    – bvgheluwe
    Commented Jan 24, 2016 at 21:00
  • 1
    @bvgheluwe I really was a bit confused, I think it was because I had some stuff done in background workers recently and it was async and I had to create a Task for specific code and wait until it was finished. Sometimes I think too complicated, I guess.
    – Felix
    Commented Jan 24, 2016 at 21:03
  • 1
    @goodeinstein Don't you get me? o.O I HAD a problem :)
    – Felix
    Commented Jan 24, 2016 at 21:11
  • 2
    The question is fine, it just had an odd title and some bad formatting. Not sure why people would downvote instead of doing some simple edits or suggestions for improvement. Now that the edits are done and the question looks good it has -3 and an off-topic vote for no apparent reason. Commented Jan 24, 2016 at 21:44
  • @AndersForsgren I got some suggestion by some guy called schmaedeck. It's fine now, I guess :)
    – Felix
    Commented Jan 24, 2016 at 22:23

2 Answers 2

30

This is an older question but worth noting - Directory.Delete ultimately calls the RemoveDirectory Windows function, which marks the directory as to-be-deleted, but the filesystem won't actually delete it until all file handles are closed (see docs). As a result it is perfectly possible to return from Directory.Delete and find the directory still exists.

5
  • 2
    Thank you, I appreciate your up-to-date answer :)
    – Felix
    Commented May 2, 2017 at 15:58
  • Appending: while (Directory.Exists("someFolder")) Thread.Sleep(100); after the Delete but before Create worked for me. Commented May 21, 2018 at 11:26
  • I was noticing issues with creating a folder sporadically failing the unit test due to the delay in the file system. I add Refresh() to the DirectoryInfo object I was using and that fixed it. Commented Dec 24, 2018 at 18:29
  • > while (Directory.Exists("someFolder")) Thread.Sleep(100); and if something goes wrong, you loop forever... Please use a for loop with a fixed number of iterations instead and throw an exception if all the tries fail.
    – Maxence
    Commented Aug 22, 2019 at 15:46
  • 2
    You might want to consider renaming the directory and then deleting it; learn.microsoft.com/en-us/windows/win32/api/winbase/… doesn't have the same caveat about being asynchronous, so maybe that it a reliable way to delete sort-of-synchronously. Commented Mar 6, 2020 at 9:58
4

I also ran into this problem intermittently while running some integration tests that use the file system.

The "full" operation I wanted was to obtain an empty folder in which my process could perform its operations. The folder might already exist (with content) due to previous test runs, or it might not if either (a) the repo was freshly cloned or (b) I was adding new test cases.

Given this illuminating answer, I realized Directory.Delete is a truly rotten plank on which to build this operation.

So I use this now:

public static DirectoryInfo EmptyDirectory(string directoryPath)
{
    var directory = Directory.CreateDirectory(directoryPath);

    foreach (var file in directory.EnumerateFiles())
    {
        file.Delete();
    }

    foreach (var subdirectory in directory.EnumerateDirectories())
    {
        subdirectory.Delete(true);
    }

    return directory;
}

I also submitted a suggestion at the Directory.Delete doc page to add some kind of note about the underlying asynchronous nature of the method (at least on Windows, I guess). As far as leaky abstractions go, this is a pretty big leak.

2
  • wouldn't it be better to make recursive call to itself on each subDirectory?
    – mxmissile
    Commented Oct 9, 2019 at 14:23
  • 1
    @mxmissile For my purposes, no. I don't want to preserve the existing folder structure. But there's nothing wrong with that goal. Adjusting this logic to accomplish that should be pretty straightforward.
    – William
    Commented Oct 9, 2019 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.