Win2008r2: Delete corrupted directory without reboot

corruptionwindows-server-2008-r2

I have a recurring annoying issue on one of my Windows 2008 R2 Web edition servers. The drive in question happens to be a SSD, though I doubt that is relevant.

I have a script that I run when I want to redeploy website code. The first line of the script looks like this:

rmdir C:\websites\www.mywebsite-test.com /s /q

Sometimes when that line is run, it works fine, other times the directory somehow gets corrupted. When this happens, I, as administrator, get Access Denied no matter what I do to the directory- can't open it, delete it, move it (explorer or command line), take ownership, etc. I believe the reason is the directory actually doesn't exist anymore, but something about it is hanging around making it still show up in Explorer and the Command Line. If I reboot the server, when I log back in the corrupted directory is gone. Everything is fine after that: I rerun my script, the first line responds with the directory cannot be found warning, and then it continues and deploys as normal.

This has happened the last two times I ran this script. The same script previously ran without any problems. I have another script that deploys the production code which has the same first line except without the "-test". I've never had the problem with the prod script, though it gets run less often. Both destination directories are IIS 7.5 application directories.

I'd like to know is there anything I can do to the corrupted directory when it happens so that I don't have to reboot? What is Windows doing on reboot that is finally making the directory go away? I'd like to simulate that without the reboot. Rebooting isn't a huge deal, but taking the entire server down for 60 seconds because one of my test directories is messed up is pretty annoying.

Of course a related question is why is this happening in the first place?

Best Answer

The directory isn't "corrupted". You have an open handle to an item at or below that directory and until the handle is closed the delete can't complete.

You can use the SysInternals handle.exe utility to list open handles on the machine and locate the process with the open handle. If you can get that process to close the handle (or just terminate the process) you'll see the delete complete.