How To Clear All Items From Cache

if (Cache.Count > 0)
{
	string currentKey = string.Empty;
	System.Collections.IDictionaryEnumerator cacheContents =
	System.Web.HttpContext.Current.Cache.GetEnumerator();

	Response.Write(string.Format("Following {0} cache keys were cleared:<br /><br />", Cache.Count));

	while (cacheContents.MoveNext())
	{
		currentKey = cacheContents.Key.ToString();
		Response.Write(currentKey + "<br />");
		System.Web.HttpContext.Current.Cache.Remove(currentKey);
	}
}
else
{
	Response.Write("Nothing to clear. Cache is empty.");
}
If you liked this post, 🗞 subscribe to my newsletter and follow me on 𝕏!