How to remove all items from cache memory

Some times you may want to remove all items from cached memory and fill it by new data.

Session State has a method for emptying Session memory for specified user (Session.Abandon() ).

but in Cache object there is no the same method. with simple technique you can do it.

IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();

while (enumerator.MoveNext())

{

    HttpContext.Current.Cache.Remove(enumerator.Key);

}

Note that, this  technique is not efficient for pages with OutputCache type. for clearing this type cached memory, you should use below code line.

HttpRuntime.Close();

Have a good time.

3 Comments

Comments have been disabled for this content.