We’ve explained in excruciating detail why you should always try to recycle IIS application pools to restart misbehaving applications, instead of restarting IIS or using IISRESET.
That said, one reason why people chose to IISRESET instead instead is …
They don’t feel like they have a simple way to find, and recycle the right application pool when things go wrong.
Especially when S.H.T.F., the site is down and you have precious seconds to act. Of course, resetting IIS this way can can cause it to S.H.T.F. much harder (including minutes of downtime and potentially taking it down).
So, in this post I’ll share simple commands I use to do just that, including:
- Recycle application pools hosting apps at a specific URL,
- Recycle all application pools for a specific website,
- Recycle pools with long-running/stuck requests,
- Recycle the pool for a specific W3WP.EXE that’s causing trouble on the server,
- And a few other nifty ones.
These commands are very easy to use, don’t require powershell scripting, and are always available on your server.
1. How to recycle a specific IIS application pool
Let’s start with the simplest one. You know the specific application pool that needs recycling, so you can recycle it like so:
%windir%\system32\inetsrv\appcmd recycle apppool TestApp
Easy peasy. Let’s get into the more fun ones next.
2. How to recycle a specific IIS worker process (w3wp.exe)
You have a specific IIS w3wp process that’s causing trouble, consuming a lot of memory, has high CPU, or is hung, etc. You have the PID for the worker process, but not it’s application pool name.
However, you want to recycle the application pool because this gives you the benefits of overlapped recycling, with zero downtime and no requests lost.
%windir%\system32\inetsrv\appcmd list wp /wp.name:24200 /xml | %windir%\system32\inetsrv\appcmd recycle apppool /in
What’s going on here? We find the worker process with the specified PID, and pipe the XML output into the “recycle apppool” command which recycles the pools corresponding to the worker process we piped in. Don’t forget the /XML output flag on the source command, and /IN on the recycle command.
P.S. It’s often a bad idea to recycle IIS worker processes with high CPU usage as a result of heavy traffic, see the reason why in our Restart and Recycle IIS guide.
3. How to recycle the app pool hosting apps for specific URL
This is a useful one. You are having trouble with a specific URL in your website, and you want to recycle the pool hosting the application(s) that are serving that url.
Of course, you should always try to diagnose the hang and fix the root causes instead of just recycling, because otherwise the issues are likely to return AND you lose the opportunity to diagnose them by recycling.
However, if you are in a pinch or you have already diagnosed …
You can recycle the right apppools like this:
%windir%\system32\inetsrv\appcmd list app http://localhost:8990/test.aspx /xml | %windir%\system32\inetsrv\appcmd recycle apppool /in
The cool part here is that we can find the IIS applications that match a particular url … then pipe them to the recycle application pool command to do the rest.
4. How to recycle IIS application pools with slow or blocked requests
Say you know your server has blocked requests, but not sure which application pools are involved. Or, there are just too many of them.
Instead of resetting the entire server, you just want to recycle all the pools that are having trouble.
Again, this may not be a great idea unless you’ve figured out what is causing the hang, and can address it to make sure the new processes won’t all hit the same hang condition after recycle. If you’ve got LeanSentry on there, you’ve already probably gotten a diagnostic report with the details, and so you are good to go.
In any case, here is how to do this quickly:
%windir%\system32\inetsrv\appcmd list requests /elapsed:5000 /xml | %windir%\system32\inetsrv\appcmd recycle apppool /in
The command chaining is doing our magic here, by looking up any request that has lasted more than 5000ms (or whatever time you want to use) … and then recycling the right application pools.
5. How to recycle all IIS application pools for a website
Here is how you can recycle the whole IIS website, if you want to think about it this way (you are still recycling the application pools though).
%windir%\system32\inetsrv\appcmd list apps /site.name:"TestApp" /xml | %windir%\system32\inetsrv\appcmd recycle apppool /in
Similar to earlier, we are listing all the apps in the site, and then recycling all their apppools.
6. How to recycle all IIS application pools with active IIS worker processes
Think of this as IISRESET-lite, which gracefully recycles all running apps but with zero downtime, and still enables fully-warm, zero startup delay scenarios if you’ve configured our best practice application warmup setup from the Application pool warmup guide.
%windir%\system32\inetsrv\appcmd list wp /xml | %windir%\system32\inetsrv\appcmd recycle apppool /in
This one is a bit obvious, but also amazing as an alternative to any other way to reset all IIS websites that you may be using.
7-10. Recycle an application pool that’s consuming too much memory (and more)
Now we are going to get a little fancy. Just a little though.
I like the tasklist command in Windows for being able to find processes that fit a certain criteria, for example using more than a specific amount of memory in Mb.
On a web server, those processes are likely to be an IIS worker process.
(To resolve memory leaks properly, check out our guide on tuning memory usage of IIS worker processes)
What if we had a command to find any process using the tasklist filter, but then recycle the corresponding pool?
We could call this command “recyclewhere.bat”.
Here is that command:
@echo off for /F "delims=" %%R in (' tasklist /FI %1 /FO CSV /NH ') do ( set "FLAG1=" & set "FLAG2=" for %%C in (%%R) do ( if defined FLAG1 ( if not defined FLAG2 ( %windir%\system32\inetsrv\appcmd list wp /wp.name:%%~C /xml | %windir%\system32\inetsrv\appcmd recycle apppool /in ) set "FLAG2=#" ) set "FLAG1=#" ) )
Giving credit where it’s due, this is based on a helpful stackoverflow example of parsing tasklist output. We just apply it to our case, to find IIS worker processes that fit the process criteria.
Here is how to use it:
recyclewhere.bat "MEMUSAGE gt 1024000"
The string we are passing in the tasklist /FI filter string that determines which processes we are interested in. Here are some examples:
- Having a specific pool identify: “USERNAME eq IIS APPPOOL\TestApp“
- Loaded a specific application DLL: “MODULES eq ntdll*“
- Memory usage > 1Gb, expressed in kilobytes: “MEMUSAGE gt 1024000“
The tasklist command is limited in terms of filtering, so you can probably get a better version of this via powershell. If you do, be sure to post in the comments!
These are some quick and dirty IIS recycling commands that help you apply the recycle to the right application pool, instead of hammering your server with an IISRESET or a WAS service restart.
Of course, recycling your application pool is a temporary bandaid … for the proper fix, you should resolve the underlying problem instead, for example:
- Fix w3wp memory leaks with Memory diagnostics,
- Reduce w3wp CPU usage with CPU diagnostics,
- Resolve IIS and asp.net hangs with Hang diagnostics.
For more on the advantages of targeted recycling, and the costs of app pool recycling to be aware of in production, be sure to check out our comprehensive Restart, Reset, and Recycle IIS guide.