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 have any questions or comments, please post them below. If you liked this post, you can share it with your followers or follow me on Twitter!