Partager via


How do I reset the disk based BLOB cache in SharePoint 2010?

In SharePoint 2010 we have completely removed the UI for resetting the disk based BLOB cache. This means that you can only reset it through the SharePoint Object Model . Fortunately, since we invested in the SharePoint 2010 Management Shell, it's not that difficult to do. Here is some sample code to help you do this:

# A script to reset the SharePoint 2010 Disk Based Cache for BLOBs
# Example: .\Reset-Blob-Cache.ps1 https://MySiteCollection

# Loads the SharePoint 2010 PowerShell extensions so we can easily
# get a reference to our dlls.
try
{
    if(((Get-PSSnapin | foreach {$_.Name}) -contains "Microsoft.SharePoint.PowerShell") -eq $false)
{
      Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction Stop
}
}
catch
{
    Write-Host -ForegroundColor "red" "This script only works on machines with SharePoint 2010 installed.`n"
Exit
}

# Use Reflection to get a reference to our dlls.
try
{
    $spAssembly = [System.Reflection.Assembly]::GetAssembly([Microsoft.SharePoint.SPSite])
[System.Reflection.Assembly]::Load($spAssembly) | Out-Null
}
catch
{
    Write-Host -ForegroundColor "red" "Could not locate the SharePoint Assemblies.`n"
Exit
}

# You must provide a site upon which to execute the cache reset.
if ($args.length -ne 1)
{
    Write-Host "Expected 1 arguments:"
    Write-Host "<site_url>"
exit
}

$site_url = $args[0]

try
{
    $site = Get-SPSite -Identity $site_url -ErrorAction Stop
}
catch
{
    Write-Host -ForeGround 'Red' "$_"
    Write-Host -ForeGround 'Red' "Please correct the arguments and try again.`n`n"
Exit
}

$warning = "This will clear the blob cache for all sites in this application, on all WFEs in the farm. `nPress 'y' to continue:"

Write-Host -ForeGround 'Yellow' -noNewLine $warning

$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")

if ($key.Character -ine "y")
{
    Write-Host "`nYou chose not to flush the cache."
exit
}

Write-Host "`nFlushing..."
[Microsoft.SharePoint.Publishing.PublishingCache]::FlushBlobCache($site.WebApplication)

Write-Host "Flushed Cache for:" $site.WebApplication.Name