How can I manually clear ASP.NET server cache (IIS 7) on a given application/website, like what can be done in IE to clear browser cache for a given domain?
Asked
Active
Viewed 5.0k times
33
-
Would you be able to mark the answer as a real answer? Thanks :) – benmccallum Feb 19 '16 at 04:55
2 Answers
49
Use the following to remove all objects from the cache
IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();
while (enumerator.MoveNext())
{
HttpContext.Current.Cache.Remove((string)enumerator.Key);
}
Also, it is a bit of a sledgehammer option but you can restart the entire application as follows:
System.Web.HttpRuntime.UnloadAppDomain();

benmccallum
- 1,241
- 12
- 27

NakedBrunch
- 48,713
- 13
- 73
- 98
-
-
@Chris Mansic: It's there. I've used it before. Take a look here at MSDN: http://msdn.microsoft.com/en-us/library/system.web.httpruntime.unloadappdomain.aspx – NakedBrunch Mar 11 '11 at 16:24
-
2Need to cast enumerator.Key to a string since Remove takes string and all keys are strings – GameSalutes May 28 '15 at 19:14
12
I had the same problem, and recycling the application pool helped me. All my caches immediately reloaded as I wanted.
Ways of recycling the application pool are described here.

Abel
- 56,041
- 24
- 146
- 247

Niklas Wulff
- 3,497
- 2
- 22
- 43
-
1You should add a path on _How to recycling the Pool_. Anyway, could find [here](https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/applicationpools/add/recycling/) – fiskolin May 05 '20 at 17:53