title: How to clear whole Windows Azure Appfabric Cache on WebRole start?
body: <p>I'm trying to do what might be a simple thing, but i just can't find the answer.
  I know how to delete cache on the application level (for a website).</p>
  
  <p>But i need to CLEAR ALL THE CACHE of my azure appfabric cache on webrole start.</p>
  
  <p><strong>EDIT : What if it can be done ? Considering a severe misfunction in Azure Asp.net shared caching or am I dumb ?</strong> </p>
  
  <p>This this the scenario i'm stuck into :</p>
  
  <ul>
  <li>I have a a WebRole which have few different websites</li>
  <li>Consider all my websites have a default.aspx file on each root with a 60 minutes caching, this is plain regular &lt;%@ OutputCache Duration="3600" VaryByParam="*" %>, no fancy code</li>
  <li>I browse them, consequently several cached pages are stored in appfabric caches for 60 minutes</li>
  <li>Consider uner the hood in this deployment cache depencies are created upon a path like 'E:\sitesroot\xx'</li>
  <li>In between, my Webrole reset or I upload a new deployment</li>
  <li>Now consider my websites in newly created instances have this path : 'F:\sitesroot\xx'</li>
  <li>You get it, heres the result I have :</li>
  </ul>
  
  <p>Directory 'E:\sitesroot\12' does not exist. Failed to start monitoring file changes.</p>
  
  <pre><code>System.Web.HttpException (0x80070003): Directory 'E:\sitesroot\12' does not exist. Failed to start monitoring file changes.
  at System.Web.FileChangesMonitor.FindDirectoryMonitor(String dir, Boolean addIfNotFound, Boolean throwOnError)
  at System.Web.FileChangesMonitor.StartMonitoringPath(String alias, FileChangeEventHandler callback, FileAttributesData&amp; fad)
  at System.Web.Caching.CacheDependency.Init(Boolean isPublic, String[] filenamesArg, String[] cachekeysArg, CacheDependency dependency, DateTime utcStart)
  at System.Web.Caching.CacheDependency..ctor(Int32 dummy, String[] filenames)
  at System.Web.Caching.OutputCache.HasDependencyChanged(Boolean isFragment, String depKey, String[] fileDeps, String kernelKey, String oceKey, String providerName)
   at System.Web.Caching.OutputCache.Get(String key)
   at System.Web.Caching.OutputCacheModule.OnEnter(Object source, EventArgs eventArgs)
  at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>It took me some times to understand that this issue MIGHT BE because the current appfacbric cache relied on a cachedepency with a hard path "E:\sitesroot\". What if this path change, has it DOES.</p>
  
  <p>So what can I do ? I'm lost here, I tried to clear appcache on Global.asax Application_Start, but this does not seem to work (and i don't understand why it does not).</p>
  
  <p>Here's my code to clear cache on each websites Global.asax Application_Start</p>
  
  <pre class="lang-vb prettyprint-override"><code>Public Shared Sub cacheClear()
  
      Dim keys As New List(Of String)()
      ' retrieve application Cache enumerator
      Dim enumerator As IDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator()
      ' copy all keys that currently exist in Cache
      While enumerator.MoveNext()
          keys.Add(enumerator.Key.ToString())
      End While
      ' delete every key from cache
      For i As Integer = 0 To keys.Count - 1
          HttpRuntime.Cache.Remove(keys(i))
      Next
  
  End Sub
  </code></pre>
  
  <p>This should do the trick, it does not seems to.</p>
  
  <p>There certainly a ridiculous point i'm missing.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I ended dropping completely asp.net outputcache provider for appfabric and writing a custom cache with appfabric. Works great, few line of codes and cachedependancy on azure made easy.</p>