Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bizarre directory delete behaviour on SSD drive

Directory c:\test has 50 or so files in it, no subdirectories.

    If IO.Directory.Exists("C:\test") Then         IO.Directory.Delete("C:\test", True)     End If      IO.Directory.CreateDirectory("C:\test") 

Drive C is Intel's X25-M80 SSD drive, OS is Windows 7 64 bit with TRIM support, Visual Studio is 2008 with target framework 3.5. When above code is executed, CreateDirectory breaks code execution without an (visible) exception. After much headache I found that Delete is not yet done by the time code execution gets to CreateDirectory. If I modify my code like this:

    If IO.Directory.Exists("C:\test") Then         IO.Directory.Delete("C:\test", True)     End If     Threading.Thread.Sleep(2000)     IO.Directory.CreateDirectory("C:\test") 

then everything works as expected.

My questions beside the obvious WTF here are:

  • shouldn't IO.Directory.Delete be a blocking function call no matter what the drive is
  • is SSD "cheating" on delete due to enabled TRIM support?
like image 552
Vnuk Avatar asked May 16 '11 20:05

Vnuk


1 Answers

I've had problems with this before, but this is not specific to SSD drives. You would be far better off doing a move then delete:

if(Directory.Exists(dirpath)) {     string temppath = dirpath + ".deleted";     Directory.Move(dirpath, temppath);     Directory.Delete(temppath, true); } Directory.Create(dirpath); 

The other way to deal with it is to loop until complete:

if(Directory.Exists(dirpath)) {     Directory.Delete(dirpath, true);     int limit = 100;     while(Directory.Exists(dirpath) && limit-- > 0)         Thread.Sleep(0); } Directory.Create(dirpath); 
like image 156
csharptest.net Avatar answered Oct 13 '22 11:10

csharptest.net