2

Documentation describes how to delete all cached objects using the IIS Manager's Application Request Routing Cache applet (details here: http://learn.iis.net/page.aspx/576/delete-cached-objects/). How might this be done from the command line or powershell script?

In my case I do have a local disk cache and deleting the files in that folder doesn't cause the files to be re-calculated. What else is required?

Geoffrey McGrath
  • 1,663
  • 1
  • 14
  • 35

2 Answers2

1

The following C# code sample is a simple console program that can delete all contents of the cache, or delete a specific URL that you wish to clear from the cache.

In order for this to compile, you will need to reference the Microsoft.Web.Administration DLL which I found in the GAC (though I also believe it is part of the Windows SDK as well).

using Microsoft.Web.Administration;
using System;

namespace IISCacheManager
{
  class Program
  {
    static void Main(string[] args)
    {
      if (args.Length != 1)
      {
        Console.WriteLine("Incorrect Usage: Specify the URL that you want to flush the cache for. To delete everything, specify 'ALL'.");
        Environment.Exit(1);
      }

      var url = args[0] == "ALL" ? string.Empty : args[0];

      var m = new ServerManager();
      var x = m.GetApplicationHostConfiguration().GetSection("system.webServer/diskCache");
      var method = x.Methods["FlushUrl"].CreateInstance();
      method.Input.SetAttributeValue("url", url);
      method.Execute();
      Console.WriteLine("Item flushed successfully.");
    }
  }
}

Hope this helps!

Paul
  • 617
  • 6
  • 14
  • 1
    Also see: http://www.paulmiles.net/2013/03/clearing-arr-cached-content.html I'm getting random access denied errors (most of the time it works). Do you experienced the same? – Piedone Jul 07 '14 at 19:44
  • We had this happen on our server as well. In our case, we had two application pools running. The cache clear request is processed by both. One had permission to that folder and the other didn't. You can fix the permissions pretty easily, but once you do that you'll get a not found error because one app pool deletes it and the other then can't find it. So it is best to make it so there is just one app pool running. – Paul Jul 12 '14 at 14:23
  • Hmm, I'll try because in my case there are also three app pools (it's an Azure Cloud Service, two are the default ones). I don't think for me it's caused by those app pools though: now I think that access denied happens if I try to (programmatically) remove a lot of entries quickly. Will see if I can deduce something and come back. – Piedone Jul 17 '14 at 13:43
  • 1
    OK I found out that what's causing accessed denied errors for me is that there are multiple threads (coming from different web API requests) simultaneously removing cache entries. Now this causes errors if one of the requests tries to remove entries already removed by another request. I don't know why this causes an access denied (instead of a not found) but I'll try to investigate. – Piedone Jul 28 '14 at 17:29
0

The best option I can come up with is to call iisreset.exe from an elevated command prompt, and then delete the contents of the cache folder, for example:

iisreset
cd d:\cache
FOR /R %d IN (.) DO rmdir /s /q "%d"

It's a little ugly--surely there must be something more elegant.

Geoffrey McGrath
  • 1,663
  • 1
  • 14
  • 35