Updated Apr 21 2021 :: by Alex Yumashev

If you're using OutputCache directive like this:

[OutputCache(Duration = 600, VaryByParam = "*", Location = OutputCacheLocation.Server)]

Or storing custom data in HttpRuntime.Cache like this


You probably caught yourself wondering

How can I flush/remove everything saved in my cache?

Some people recommend iterating through the items in HttpRuntime.Cache but this will only give you the custom items you stored, not the pages.

I've spent some time lurking through ASP.NET reference source and finally came up with a working solution.

public static void ClearAllCache()
    var runtimeType = typeof(System.Web.Caching.Cache);
    var ci = runtimeType.GetProperty(
       BindingFlags.Instance | BindingFlags.NonPublic);
    var cache = ci.GetValue(HttpRuntime.Cache) as System.Web.Caching.CacheStoreProvider;
    var enumerator = cache.GetEnumerator();
    List<string> keys = new List<string>();
    while (enumerator.MoveNext())
    foreach (string key in keys)

The code uses reflection to get to the "InternalCache" property, then cast it to CacheStoreProvider and then iterate through the keys there. Once you have the list of keys, simply remove them one by one.

'Clear/Flush all OutputCache in ASP.NET' was written by Alex Yumashev
Alex Yumashev
Alex has founded Jitbit in 2005 and is a software engineer passionate about customer support.

Subscribe comments Tweet