Set IIS Application Pool recycle defaults to Specific Times, not Regular Time Interval

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).
Published on Friday, 3 April 2015

Screenshot by Jan Reilink

Sometimes you want to change this regular time interval to a specific time schedule. 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).

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

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

a Specific Times setting translates to a scheduled recycle. An application pool recycles daily at this time. The Regular Time Interval setting, on the other hand, configures an application pool to recycle after a specified amount of time has elapsed.

Configure a periodicRestart 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:

C:\Windows\system32\inetsrv\appcmd.exe `
  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):

# all on one line
C:\Windows\system32\inetsrv\appcmd.exe `
  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. This makes all application pools to recycle at 4 A.M in the morning, suppose you would want this.

Application Pool Specific Time Interval recycle settings

Specific Times: a set of specific local times, in 24 hour format, when the application pool is recycled.

In PowerShell, set application pool Specific Times TimeSpan value

If you want to use a more PowerShell solution to set an application pool to recycle on a specific time (instead of a regular 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 Specific Times recycle value of a specific Applicaton Pool using PowerShell's Get-ItemProperty

If you have set one or more specific times recycle values scheduled for an application pool, 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 four (4) hours on the hour, starting at midnight:

value
-----
00:00:00
04:00:00
08:00:00
12:00:00

Get periodicRestart schedule for all IIS application pools

A tiny PoSH script to loop through all application pools in IIS to get the periodicRestart schedule (WebAdministration module required):

Import-Module WebAdministration
$skipPools = @("DefaultAppPool", "Classic .NET AppPool", ".NET v2.0 Classic", ".NET v2.0", ".NET v4.5 Classic", ".NET v4.5")
foreach($apppool in (Get-Item IIS:\AppPools\*).Name) {
  if($skipPools.Contains($apppool)) {
    continue
  }
  
  $recycling = (Get-ItemProperty ("IIS:\AppPools\${apppool}") -Name Recycling.periodicRestart.schedule.collection) | select value
  if($recycling) {
    foreach($value in $((Get-ItemProperty ("IIS:\AppPools\${apppool}") -Name Recycling.periodicRestart.schedule.collection) | select value)) {
      write-output "${apppool} periodicRestart schedule at: $value"
    }
  }
}

This skips over the pools listed in the $skipPools array, and only lists for application pools having a schedule configured.

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 what about the IIS Manager GUI now?

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.

Reset Application Pool recycling Defaults back to periodicRestart

If you want to reset your application pool recycling defaults back to periodicRestart (Regular Time Interval), you can do so with the following appcmd command:

# Set applicationPoolDefaults.recycling.periodicRestart.time
# to 29 hours, 1 minute, or 1741 minutes
C:\Windows\system32\inetsrv\appcmd.exe `
  set config `
  -section:system.applicationHost/applicationPools `
  /applicationPoolDefaults.recycling.periodicRestart.time:"29:01:00" `
  /commit:apphost
# Set applicationPoolDefaults.recycling.periodicRestart.time
# to 29 hours, or 1740 minutes
C:\Windows\system32\inetsrv\appcmd.exe `
  set config `
  -section:system.applicationHost/applicationPools `
  /applicationPoolDefaults.recycling.periodicRestart.time:"29:00:00" `
  /commit:apphost

if the Regular Time Interval default is set to 41760 minutes / 29 days, you must reset this to a non-default value. For example 1741. When that's saved to applicationHost.config, you can reset it back to it's original regular time interval of 1740 minutes.