How to really clear the BlobCache on all servers in the farm

Whoever worked with the BlobCache has wanted to clear it someday (especially when developing or debugging issues).  The BlobCache is the caching mechanism that keeps a local copy of large files.  For example, your pictures, styles, and scripts, can be cached on each front-end.  This will reduce SQL traffic.

 

The only visual way to clear the BlobCache is to navigate to https://mywebapp:myport/_layouts/objectcachesettings.aspx and click on "Force this server to reset its disk based cache".  Unfortunately, this only works for the server you are on, which is very interesting in a load-balanced scenario (you'll need to use a host file and point to each server, turn by turn, and navigate to the web site).

 

Then, if you go about reading on Object Caching at https://msdn.microsoft.com/en-us/library/aa622758.aspx, you will notice that there is a note on BlobCache where you can execute the following command to clear the BlobCache "stsadm -o setproperty -propertyname blobcacheflushcount -propertyvalue 11 -url https://mywebapp:port".  The note is unclear whether this should work for all servers or just one, but it works for all server ...  Unfortunately, it works once!  The reason is simple, the BlobCacheFlushCount has to be incremented each time.  Now this isn't very practical if you want to script it!

 

If you look at the Disk Based Caching for Binary Large Objects, https://msdn.microsoft.com/en-us/library/aa604896.aspx, you notice that it mentions you can simply check "Force all servers in the farm to reset their disk based cache".  This would be great but where is that CheckBox??? If you look at the ObjectCacheSettings.aspx file (in Layouts), you will indeed find such a checkbox.  If you use Reflector to open the class behind the page, you'll notice that, in the OnLoad method, the checkbox's visible property is set to False!  Isn't that practical...

 

On the other hand, by using Reflector, you can see that the available CheckBox calls a FlushCache() method from the BlobCache class, but that one is internal.  But you can also notice that the invisible CheckBox's code is available and is actually quite simple.  It does what the STSADM command executes, but it increments the value.  Here's the code that allows you to clear the BlobCache on demand and for all servers:

    1: SPSite site = new SPSite("https://mywebapp:myport");
    2: string s = "0";
    3:  
    4: if (site.WebApplication.Properties.ContainsKey("blobcacheflushcount") && site.WebApplication.Properties["blobcacheflushcount"] != null)
    5:     s = site.WebApplication.Properties["blobcacheflushcount"] as string;
    6:  
    7: site.WebApplication.Properties["blobcacheflushcount"] = (int.Parse(s, CultureInfo.InvariantCulture) + 1).ToString(CultureInfo.InvariantCulture);
    8: site.WebApplication.Update();
    9: site.Dispose();

 

I've also created a small Windows application that does just that, or you can create your own custom STSADM extension to do just the same thing.  You can download the application here :

 

Update: I’m also happy to mention that Sean McDonough created a Feature deployed through a WSP that does the same thing with a more convivial web interface.  It can be found on Codeplex at this address : https://blobcachefarmflush.codeplex.com/.

 

Maxime