Clearing Your Media Cache in Azure Isn't as Simple As It Sounds, Thanks to This Little Kudu Access Issue.

During our latest release there was a need to clear our media cache. Normally I'd be happy to do this by just restarting the app, but in production that method is not satisfactory. After a few twists and turns it became clear the only way to do this without a restart is to write a custom script.

Now, normally you can just delete contents of a certain folder, which would be specified under the setting, “Media.CacheFolder”. If you want to check this setting, just go to /sitecore/admin/showconfig.aspx on your Content Management instance. The usual location would appear as this:

<setting name="Media.CacheFolder" value="/App_Data/MediaCache"/>

 But in Azure, you're most likely going to see a path on the D drive, which is not accessible through FTP. 

<setting name="Media.CacheFolder" value="d:\local\MediaCache" patch:source="Sitecore.Settings.Azure.config"/>  

 

Kudu to the Rescue?

Kudu is a great tool for your Azure instance management, but it looks like we found a little issue here. It does not have access to the right instance that your media cache folder is on. As you can see below, the local folder has the specified folder missing. 

 

A Workaround

The page below can be deployed to your Azure instance and run to either review or delete the contents of this pesky folder we're trying to clear.

Important - The Boolean deleteNow must be observed before you run this page. It's not really destructive to clear this folder at any time, but in production I'm always checking things twice when I have to manually do something.

 This file is also available for download here.


<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
    private bool deleteNow = false; // This will just report the files found if set to false
    private string dirPath = @"d:\local\MediaCache"; // Set this to the folder you want cleared.
    protected void Page_Load(object sender, EventArgs e)
    {
        if (deleteNow){
            ActionNotice.Text = "<p class='Warning'>The action is set to delete when this button is clicked</p>";
        }
        else{
            ActionNotice.Text = "<p>This page is set to only show the files to be deleted when the button is clicked. Set deleteNow to true for action to be taken.</p>";
            actionButton.Text = "Show Azure Media Cache Contents";
        }
    }
    protected void actionButton_Click(object sender, EventArgs e)
    {
        if (deleteNow){
            this.actionButton.Visible = false;
        }   
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(dirPath);
        Output.Text = "<ul>";
        if (!dir.Exists)
        {
            Output.Text += "<li>Directory " + dirPath + " does not exist.</li>";
            return;
        }
        foreach (System.IO.DirectoryInfo subDir in dir.GetDirectories())
        {
            if (deleteNow)
            {
                Output.Text += "<li><strong>Deleting directory " + subDir.Name + "</strong></li>";
                subDir.Delete(true);
            }
            else
            {
                Output.Text += "<li><strong>Will delete directory " + subDir.Name + "</strong></li>";
            }
        }
        foreach (System.IO.FileInfo file in dir.GetFiles())
        {
            try
            {
                if (deleteNow)
                {
                    file.Delete();
                    Output.Text += "<li>Deleting file " + file.Name + "</li>";
                }
                else
                {
                    Output.Text += "<li>Will delete " + file.Name + "</li>";
                }
            }
            catch (Exception ex)
            {
                Response.Write("Cannot delete file " + file.Name + " (" + ex.ToString() + ")");
            }
        }
        Output.Text += "</ul>";
    }
</script>
   
        <title>Clear Azure MediaCache Folder</title>
        <link rel="shortcut icon" href="/sitecore/images/favicon.ico">
        <style type="text/css">
            body { font-family: 'Open Sans', Arial, sans-serif; font-size: 11pt; }
            button, input, optgroup, select, textarea { font-family: inherit; }
            .Warning { color: red; }
        </style>   
    
        <form runat="server" id="form">
            <asp:literal runat="server" id="ActionNotice" enableviewstate="false"></asp:literal>
            <asp:button runat="server" id="actionButton" text="Clear Azure MediaCache Folder" onclick="actionButton_Click"></asp:button>
            <br>
            <br>
            <hr>
            <br>
            <asp:literal runat="server" id="Output"></asp:literal>
        </form>