Can't delete files blocked by IIS

I have a poweshell command that removes a folder (i.e. Summer) from the wwwroot directory and recreates the folder with the required files (images, css, dll, etc.). The problem is that IIS tends to block some images or files in the directory, so the powershell command doesn't delete the file. I recycle / stop the application before running the powershell script that the site is using, but the problem still persists. This problem is random, that is, the powershell script might delete the folder someday while it might not be at another time. It's strange, if I start deleting the contents (subfolders, files) inside Summer, at the end, I can delete the Summer folder, but it's a manual process and tedious.

Is there any command I can add to the powershell file or batch file to delete the "Summer" folder even though it is locked by IIS?


source to share

3 answers

You can use IIS command line cmdlets to start and stop application pools, websites, etc.

Import-Module WebAdministration;
Stop-WebAppPool ${appPoolName}
Stop-WebSite ${webSiteName}


you can then run them again using opposite commands

Start-WebAppPool ${appPoolName}
Start-WebSite ${webSiteName}




I agree with @Lynn Crumbling and recommend iisreset.

Sysinternals has two tools that provide other options:

The ProcExp tool allows you to determine which processes have open handles for a given file and allows you to close that handle. The downside to this tool is that it is not a command line tool.

MoveFile tool allows you to schedule file deletion after reboot.



As stated in the comment, stopping IIS completely using iisreset stop will work.

Also, you can only stop the application from which you are trying to delete files. Check out the Administrator's Guide .



All Articles