Clear Sitecore Media Cache in Azure Web Apps

Hello Sitecore enthusiasts,

As we all are aware Most of the Sitecore Applications nowadays are hosted in Azure Web Apps. Here is a Sitecore blog which explains the advantages of hosting Sitecore Applications in Azure Web Apps
Five reasons why Microsoft Azure Web Apps and Sitecore are a match made in heaven

With all these advantages, still many people face difficulties like debugging in Azure Web Apps, facing challenges in Media Cache, Dictionary items etc, as Azure Web Apps handles those in different way.
For learning about Basics of Debugging your application in Azure Web Apps, please refer to my previous blog :
Basics of Debugging Sitecore Application using Azure and Kudu Console

Here we are going to discuss an issue caused due to Media Caching and the approach we followed to get it solved.

All of us might have faced issues related to Media Cache in our Sitecore Web Applications. Either it is CM server or CD server, we would have faced a situation which resulted in clearing Media Cache and then finally images started displaying as expected. For us also, our environment was hosted in Azure Web Apps, we had logo missing from our website and the moment we cleared the Media Cache and reloaded our website, the logo started to display.

In a standalone Sitecore instance we all know where the Media Cache is stored. You can find those under “App_Data\MediaCache” folder in your root. You can also find the path from the following settings : <setting name=”Media.CacheFolder”.

But in Azure Web Apps, you do not have Media Cache stored in App_Data folder but it is stored somewhere else.
You can get the default path from the setting:

Media cache folder path config

But we cannot navigate to this folder through the KUDU console as we cannot find the folder named local in the D directory.
Now suppose you have to clear Media Cache because of one of the issues or the other which I had described above. Unfortunately the usage of /sitecore/admin/cache.aspx does not clear the media cache for you. Coming back to the issue which I described earlier, I have a logo missing from my page. So just to get back the logo why should I clear the entire Media Cache folder. Even though it gets cached again immediately an image is called up again, but still it is not required to clear all the Media cache for just one or two images.

So I am sharing the approach we used to check whether the Media cache is available using the ID of the Image (in my example it is logo) and cleaning that particular folder only using an admin page.

The code explains itself:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ClearMediaCache.aspx.cs" Inherits="SitecoreProject.sitecore.admin.MediaCache" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" />
</div>
<br />
<br />
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Files List" />
<br />
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Clear Media Cache" />
<br />
<br />
</div>
<div>
<asp:Label ID="Label1" runat="server" />
</div>
</form>
</body>
</html>
view raw ClearMediaCache.aspx hosted with ❤ by GitHub
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;
using Sitecore.Resources.Media;
using Sitecore.Sites;
namespace SitecoreProject.sitecore.admin
{
public partial class MediaCache : System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Button1_Click (object sender, EventArgs e)
{
Label1.Text = "";
StringBuilder sb = new StringBuilder ();
string id = TextBox1.Text;
var files = GetFilesList (id);
foreach (var file in files) {
var parentDirectory = Directory.GetParent (file).FullName;
sb.AppendFormat ("Filename is {0}", file);
sb.Append ("</br>");
sb.AppendFormat ("Parent Directory is {0}", parentDirectory);
sb.Append ("</br>");
sb.Append ("</br>");
}
Label1.Text = sb.ToString ();
}
protected void Button2_Click (object sender, EventArgs e)
{
Label1.Text = "";
StringBuilder sb = new StringBuilder ();
string id = TextBox1.Text;
var files = GetFilesList (id);
foreach (var file in files) {
sb.AppendFormat ("Filename is {0}", file);
sb.Append ("</br>");
var parentDirectory = Directory.GetParent (file).FullName;
sb.AppendFormat ("Deleting Parent Directory");
Directory.Delete (parentDirectory, true);
}
Label1.Text = sb.ToString ();
}
private List<string> GetFilesList (string id)
{
var database = Sitecore.Data.Database.GetDatabase ("master");
var mediaItem = new MediaItem (database.GetItem (new Sitecore.Data.ID (id)));
var media = Sitecore.Resources.Media.MediaManager.GetMedia (mediaItem);
var mediaId = media.MediaData.MediaId;
var mediaCachePath = Sitecore.Configuration.Settings.GetSetting ("Media.CacheFolder");
var finalPath = Sitecore.IO.FileUtil.MapPath (mediaCachePath);
var files = Directory.EnumerateFiles (finalPath, "*.*", SearchOption.AllDirectories).Where (s => s.Contains (mediaId + ".ini"));
return files.ToList ();
}
}
}

Once executing “Get Files List”, It will list all the MediaCache files which relates to that Image. After verifying the files list, if you click on “Clear Media Cache”, it will clear that Media cache folder where the particular Image Media is cached. When you execute from CD servers, please change the database to “web”, as CD servers will not have master database in connection strings.

Get Files list.pngYou can also clear its parent directory by just getting the parent of the directory which we are clearing now. It will clear the website specific media cache.

There can be other methods too which will help clear the Media Cache but this helped us solve the issues we were facing because of Media Cache.
Please feel free to share any other approaches that you might have followed for the same. You can also contact me if you face any issues in executing the same.

Happy Sitecoring 🙂

One thought on “Clear Sitecore Media Cache in Azure Web Apps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s