4

I have a poweshell command which deletes the folder(i.e. Summer) from wwwroot directory and recreates the folder with the necessary files(images, css, dll etc) in it. The problem is every once in a while the IIS tends to lock some of the images or files in the directory so the powershell command fails to delete the file. I do recycle/stop the apppool before running powershell script which is used by site but still the problem persists. This issue is random i.e. the powershell script can delete the folder sometime while it can't other time. The weird thing is, if i start deleting the contents (subfolders, files) inside 'Summer', at the end, i am able to delete 'Summer' folder, but it is an manual process and which is tedious.

Is there any command which i can put in powershell or batch file to delete 'Summer' folder, even though when it is locked by IIS?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
sanjeev40084
  • 9,227
  • 18
  • 67
  • 99

3 Answers3

2

I agree with @Lynn Crumbling and recommend iisreset.

Sysinternals has two tools that provide other options:

The ProcExp tool allows you to find which processes have open handles to a given file, and allows you to close that handle. The downside of this tool is that it's not a command line tool.

The MoveFile tool allows you to schedule the file to be removed after reboot.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
2

You can use the IIS powershell commandlets to start and stop app pools, web sites etc

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

you can then start them again afterwards using the opposite commands

Start-WebAppPool ${appPoolName}
Start-WebSite ${webSiteName}
martin308
  • 706
  • 6
  • 20
  • Using tinyget.exe for stress web and execute Stop-WebAppPool, state AppPool is Stopping, not Stopped. Any solution about it? – Kiquenet Jan 08 '14 at 07:10
  • i'll try with `Stop-WebAppPool`, i know that files still get locked with `Stop-Website`, thanks! – sonjz Jul 25 '17 at 20:25
0

As put in comment, fully stopping IIS using iisreset stop would work.

Also, you may want to stop only the application from which you are trying to delete files from. Look at the Administration Guide.

Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40