Delete All Cached Objects in ARR Cache from Comman

2019-05-24 12:11发布

问题:

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?

回答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!



回答2:

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.