By default, an IIS application pool (or “AppPool”) recycles on a regular time interval of 1740 minutes, or 29 hours. One reason for this time interval is that application pools don’t recycle at the same moment every day (every day at 07.00 for example). However, sometimes you may want to change this regular time interval to specific times. And when you try to configure this in IIS Manager, it gives you an error. Luckily, AppCmd and PowerShell come to the rescue!

IIS Application Pool recycling defaults

Recycling means the worker process that handles requests for an application pool is terminated and a new worker process is started.

Why is IIS’ default application pool recycling set to 1740 minutes? One reason for this time interval is that application pools don’t recycle at the same moment every day, and 29 hours for the simple reason that it’s the smallest prime number over 24. A staggered and non-repeating pattern that doesn’t occur more frequently than once per day, “you don’t get a resonate pattern” (Source: Scott Forsyth’s Blog).

This may interest you:   How to detect ethernet network speed in Windows

Imagine your IIS application pool (AppPools) are configured to recycle on a regular time interval of 1740 minutes, and you want to change this setting through Internet Information Services (IIS) Manager to a time interval. You either browse to an application pool and its Advanced Settings, or you try to set Application Pool Defaults.

When you set the Regular Time Interval (minutes) to 0, an error message pops up:

AppPool IdleTime-out property error
AppPool IdleTime-out property error

The ‘Idle Time-out (minutes)’ property of the application pool’s process model must be less than the Regular Time Interval (minutes)’ property of the application pool’s period restart.

Setting the AppPool Idle Time-out property to zero (0) -because you want to set the Regular Time Interval to 0- may result in unwanted AppPool and website behavior… This is a known bug in the IIS Manager GUI.

Appcmd to configure application pool recycle on Specific Times and set Regular Time Interval to 0

Fortunately you can use AppCmd to configure these Regular Time Interval and Specific Times Application Pool Defaults settings. First you have to set the Regular Time Interval to 0:

AppCmd set config
 -section:system.applicationHost/applicationPools
 /applicationPoolDefaults.recycling.periodicRestart.time:"00:00:00"
 /commit:apphost

Next, you’ll configure a Periodic Restart Schedule (4 A.M):

AppCmd set config
 -section:system.applicationHost/applicationPools
 "/+applicationPoolDefaults.recycling.periodicRestart.schedule.[@0,value='04:00:00']"
 /commit:apphost

The @0 numeric index is used so more than one periodic restart schedules can be created (not sure this is necessary). This makes all application pools to recycle at 4 A.M in the morning, suppose you would want this.

This may interest you:   .svc WCF web service returns 404 Not Found on IIS 8

In PowerShell, set application pool regular time interval

If you want to use a more PowerShell solution to set an application pool to recycle on a regular time interval, you can use the following PS snippets:

Set-WebConfiguration `
  /system.applicationHost/applicationPools/applicationPoolDefaults/recycling/periodicRestart `
  -value "0"

And then you add a specific time for the application pool to recycle on (note the add-, not set-):

Add-WebConfiguration ` 
  /system.applicationHost/applicationPools/applicationPoolDefaults/recycling/periodicRestart/schedule `
  -value (New-TimeSpan -h 4 -m 00)

Verify your newly made settings:

Get-WebConfiguration `
  /system.applicationHost/applicationPools/applicationPoolDefaults/recycling/periodicRestart/schedule/add ` 
  | select value

Get the periodic restart recycle value of a specific Applicaton Pool using PowerShell’s Get-ItemProperty

If you have set one or more periodicRestart recycle values for an application pool, you can use the following PowerShell command to get the values:

(Get-ItemProperty ('IIS:\AppPools\example.org') -Name Recycling.periodicRestart.schedule.collection) | select value

The output is for example set to every 6 hours on the hour, starting at midnight:

value
-----
00:00:00
06:00:00
12:00:00
18:00:00

Get recycling.periodicRestart.schedule using appcmd:

appcmd.exe list apppool example.com /config

The appcmd command above will list the current application pool ‘example.com’ configuration, including its scheduled periodicRestart. Substitute /config with /text:* for all configuration settings for that pool.

And the IIS Manager GUI?

After setting the regular time interval to 0, you may use the IIS Manager GUI to configure Application Pool Defaults and a Periodic Restart schedule.