How to clean the configuration cache across the farm
Sometimes cleaning the SharePoint configuration cache is required. This is a rather simple endeavour:
- stop the SharePoint timer service
- clean all the cache files (
*.xml
, located under%ProgramData%\Microsoft\SharePoint\Config\[some-guid]
) - modify the
cache.ini
in the same folder - start the SharePoint timer service
This, however, is a somewhat tedious process for every thing other than single-server farms.
The following script will perform the cache cleanup on every single server in the farm using PowerShell remoting. (This means PowerShell remoting needs to be enabled on every server in the farm and the current user needs access to every server in the farm.)
#requires -pssnapin Microsoft.SharePoint.PowerShell
<#
.SYNOPSIS
Cleans the cache across the whole Farm.
All servers will be accessed via PowerShell remoting. Make sure PowerShell remoting is enabled.
.EXAMPLE
.\Clean-SPFarmCache.ps1
Does all the hard work
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[string[]]$Servers,
[switch]$Force
)
$errorActionPreference = "Stop"
if ($Servers -eq $null -or $Servers.Length -eq 0) {
$Servers = (Get-SPFarm).Servers | ? { $_.Role -ne "Invalid" } | select -ExpandProperty Address
}
if(-not $Force.isPresent -and -not $PSCmdlet.ShouldContinue("Clean Cache for $($servers) ?", "Confirm")) {
return
}
$Servers | % {
if(-not $PSCmdlet.ShouldProcess($_)){
return
}
$script = {
Stop-Service SPTimerV4
$caches = Get-ChildItem -Path "$($env:ProgramData)\Microsoft\SharePoint\Config\" -Recurse -Filter cache.ini | ? {$_.Directory.Name -ne "$([guid]::Empty)"}
$caches | % {
$d = $_.Directory
Get-ChildItem -Path $d.FullName -Exclude *.ini | Remove-Item -Force
Set-Content -Path "$($d.FullName)\cache.ini" -Value "1" -Encoding UTF8
}
Start-Service SPTimerV4
}
Invoke-Command -ScriptBlock $script -ComputerName $_
}