How to Clear ASP.NET Web Cache

In ASP.NET you can remove an item from the web cache by invoking Cache.Remove(string ID). But, what happens when, for some reason, you want to quickly clear the whole cache? There’s no Clear or RemoveAll method in System.Web.Caching.Cache object.

WORKAROUND

You can iterate over all the entries and remove them by their key (or ID), using the GetEnumerator method of the Cache class.

IDictionaryEnumerator enumerator = Cache.GetEnumerator();

while(enumerator.MoveNext())
{
Cache.Remove(enumerator.Key)
;
}

3 thoughts on “How to Clear ASP.NET Web Cache”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.