How to Disable Static Content Caching in IIS 7: Complete Guide for Developers


4 views

When developing web applications on IIS 7 (Windows Server 2008 R2), many developers encounter aggressive static content caching that persists even after file updates. The server continues serving old versions of .html and .js files until you perform an iisreset - which isn't practical during active development.

Most documentation suggests these common approaches that often don't work:

  • Browser cache clearing (not the real issue)
  • OutputCache module configuration
  • ApplicationHost.config modifications
  • Appcmd cache disabling commands

1. Kernel-Mode Cache Elimination

Add this to your web.config:

<system.webServer>
  <caching enabled="false" enableKernelCache="false">
    <profiles>
      <add extension=".html" policy="DisableCache" />
      <add extension=".js" policy="DisableCache" />
    </profiles>
  </caching>
</system.webServer>

2. HTTP Response Header Override

Create a custom IIS module or add this to Global.asax:

protected void Application_PreSendRequestHeaders()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetNoStore();
    Response.AppendHeader("Pragma", "no-cache");
    Response.AppendHeader("Expires", "-1");
}

3. File Versioning Technique

Append query strings to static resources:

<script src="scripts/main.js?v=<%= DateTime.Now.Ticks %>"></script>

For server-wide changes, run this PowerShell script:

Import-Module WebAdministration
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' 
  -filter "system.webServer/caching" 
  -name "enabled" 
  -value "False"
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' 
  -filter "system.webServer/caching/@enableKernelCache" 
  -name "enableKernelCache" 
  -value "False"

After implementing changes:

  1. Check response headers for "Cache-Control: no-cache"
  2. Use Fiddler to verify no 304 (Not Modified) responses
  3. Test with direct file requests (e.g., http://server/file.js?test=1)

After spending hours battling IIS 7's aggressive static content caching on Windows Server 2008 R2, I've compiled the most effective solutions. The server stubbornly caches .html and .js files, refusing to recognize disk changes without an iisreset.

Before we dive into working solutions, let me share what didn't work for me:

  • Browser cache clearance (definitely a server-side issue)
  • OutputCache modifications via IIS Manager GUI
  • AppCmd cache disabling commands
  • Manual applicationHost.config edits

After extensive testing, this combination proved effective:

<configuration>
  <system.webServer>
    <caching enabled="false">
      <profiles>
        <add extension=".html" policy="DontCache" kernelCachePolicy="DontCache" />
        <add extension=".js" policy="DontCache" kernelCachePolicy="DontCache" />
      </profiles>
    </caching>
    <staticContent>
      <clientCache cacheControlMode="DisableCache" />
    </staticContent>
  </system.webServer>
</configuration>

For those who prefer automation:

# Disable kernel-mode cache
Set-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST" 
  -Filter "system.webServer/caching" -Name enableKernelCache -Value $false

# Disable user-mode cache
Set-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST" 
  -Filter "system.webServer/caching" -Name enabled -Value $false

# Set no-cache headers for static content
Add-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST" 
  -Filter "system.webServer/staticContent/clientCache" 
  -Name "cacheControlMode" -Value "DisableCache"

To confirm caching is disabled:

  1. Make a timestamp change to your test file (e.g., test.html)
  2. Use Fiddler or browser dev tools to check response headers
  3. Verify absence of "ETag" and "Last-Modified" headers
  4. Check for "Cache-Control: no-cache" header

If files remain cached despite these changes:

  • Check for residual worker processes (stop and start application pool)
  • Verify configuration at the correct level (site vs application)
  • Consider file system monitoring permissions for IIS_IUSRS