title: How to force BundleCollection to flush cached script bundles in MVC4
body: <p>... or <em>how I learned to stop worrying and just write code against completely undocumented APIs from Microsoft</em>. Is there any actual documentation of the official <code>System.Web.Optimization</code> release? 'cuz I sure can't find any, there's no XML docs, and all the blog posts refer to the RC API which is substantially different. Anyhoo..</p>
  
  <p>I am writing some code to automatically resolve javascript dependencies and am creating bundles on the fly from those dependencies. Everything works great, except if you edit scripts or otherwise make changes that would affect a bundle without restarting the application, the changes won't be reflected. So I added an option to disable caching of the dependencies for use in development.</p>
  
  <p>However, apparently <code>BundleTables</code> caches the URL <em>even if the bundle collection has changed</em>. For example, in my own code when I want to re-create a bundle I do something like this:</p>
  
  <pre><code>// remove an existing bundle
  BundleTable.Bundles.Remove(BundleTable.Bundles.GetBundleFor(bundleAlias));
  
  // recreate it.
  var bundle = new ScriptBundle(bundleAlias);
  
  // dependencies is a collection of objects representing scripts, 
  // this creates a new bundle from that list. 
  
  foreach (var item in dependencies)
  {
      bundle.Include(item.Path);
  }
  
  // add the new bundle to the collection
  
  BundleTable.Bundles.Add(bundle);
  
  // bundleAlias is the same alias used previously to create the bundle,
  // like "~/mybundle1" 
  
  var bundleUrl = BundleTable.Bundles.ResolveBundleUrl(bundleAlias);
  
  // returns something like "/mybundle1?v=hzBkDmqVAC8R_Nme4OYZ5qoq5fLBIhAGguKa28lYLfQ1"
  </code></pre>
  
  <p>Whenever I remove &amp; recreate a bundle <em>with the same alias</em>, absolutely nothing happens: the <code>bundleUrl</code> returned from <code>ResolveBundleUrl</code> is the same as before I removed &amp; recreated the bundle. By "the same" I mean that the content hash is unchanged to reflect the new contents of the bundle. </p>
  
  <p><em>edit</em>... actually, it's much worse than that. The <em>bundle itself</em> is cached somehow outside of the <code>Bundles</code> collection. If I just generate my own random hash to prevent the browser from caching the script, ASP.NET returns <em>the old script</em>. So, apparently, removing a bundle from <code>BundleTable.Bundles</code> does not actually do anything. </p>
  
  <p>I can simply change the alias to get around this problem, and that is OK for development, but I don't like that idea since it means either I have to deprecate aliases after each page load, or have a BundleCollection that grows in size on every page load. If you left this on in a production environment, it would be a disaster.</p>
  
  <p>So it seems that when a script is served, it gets cached independent of the actual <code>BundleTables.Bundles</code> object. So if you re-use a URL, even if you've removed the bundle that it referred to before reusing it, it responds with whatever's in its cache, and altering the <code>Bundles</code> object does not flush the cache -- so only <em>new</em> items (or rather, new items with a different name) would ever be used.</p>
  
  <p>The behavior seems odd... removing something from the collection should remove it from the cache. But it doesn't. There must be a way to flush this cache and have it use the current contents of the <code>BundleCollection</code> instead of what it cached when that bundle was first accessed. </p>
  
  <p>Any idea how I would do this?</p>
  
  <p>There is this <code>ResetAll</code> method which has an unknown purpose but it just breaks things anyway so that isn't it.</p>