Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I got a service coded in c# whoes deleted somes web site files hosted on iis, before an update. But sometime when i delete the files, they stay there.

If I try to delete them manually, via explorer, the file are not deletable, because they are in state "Delete pending".

There is the way my service try to delete the file

                  // Enlève tout les attributs sur le fichiers afin de s'assurer que le fichier n'est pas en lecture seul
                  File.SetAttributes(file, FileAttributes.Normal);

                  // Supprime le fichier

It's there a way to avoid this state ?

What can i do to force the delete by c# code?

Could i release all process to the file by c# code ?

The environnement is

IIS 7.5 Windows 2008-r2 .net 4.0


share|improve this question

migrated from Nov 11 '11 at 16:08

This question came from our site for professional and enthusiast programmers.

Why close my question ? – Cédric Boivin Nov 11 '11 at 15:35
I think it will be probably moved on serverfault – Felice Pollano Nov 11 '11 at 15:38
And now is it better ? – Cédric Boivin Nov 11 '11 at 15:46
It's could be nice people try to close the questions say why they wont see my question be close. – Cédric Boivin Nov 11 '11 at 15:48

You'll want to find out what other process has the files open. I don't know how you'd do that programatically, but speaking as a sysadmin, the handle.exe utility from SysInternals (or their Process Explorer GUI tool) can show that to you real-time.

Then you'll want to either:

  1. Avoid the condition that causes the file to be locked by the other process,
  2. Close the handle if you can do so safely.
share|improve this answer
Thanks for you answer but the question was how to do it programatically. I really do not understand why my question was migrated to server fault. – Cédric Boivin Nov 11 '11 at 16:50
Too bad; I have no idea why the folks on SO would have voted to move it here. – mfinni Nov 11 '11 at 19:23

You can get a list of files that are on that state through here I don't know how your files end up in that state but the first way that comes to my mind is to rename and then delete the file there is a similar solution for C++ on SO here

In C# it should be something like

File.Move(oldFileName, newFileName);
share|improve this answer
You think the move will unlock the file ? – Cédric Boivin Nov 16 '11 at 13:25
It renames the file. – user Nov 16 '11 at 13:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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