Clear SharePoint Config Cache with PowerShell


Add-PSSnapin -Name Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue
write-host "CLEAR CONFIG CACHE ON FARM" -fore green
$servers = get-spserver | ?{$_.role -eq "Application"}
foreach ($server in $servers)
{
$servername = $server.Address
write-host "Stop Timer Service on server $servername" -fore yellow
(Get-WmiObject Win32_Service -filter "name='SPTimerV4'" -ComputerName $servername).stopservice() | Out-Null
}

foreach ($server in $servers)
{
$servername = $server.Address
$folders = Get-ChildItem ("\\" + $servername + "\C$\ProgramData\Microsoft\SharePoint\Config")

foreach ($folder in $folders)
{
$items = Get-ChildItem $folder.FullName -Recurse
foreach ($item in $items)
{
if ($item.Name.ToLower() -eq “cache.ini”)
{
$cachefolder = $folder.FullName
}

}
}

write-host "Found CacheFolder on Server $servername = $cachefolder" -fore Yellow
$cachefolderitems = Get-ChildItem $cachefolder -Recurse

write-host "Delete all XMQL Files inside this CacheFolder" -fore Yellow
foreach ($cachefolderitem in $cachefolderitems)
{
if ($cachefolderitem -like “*.xml”)
{

$cachefolderitem.Delete()
}

}

$a = Get-Content $cachefolder\cache.ini
$a = 1
write-host "Creating a new Cache.ini File on server $servername" -fore Yellow
Set-Content $a -Path $cachefolder\cache.ini

}

foreach ($server in $servers)
{
$servername = $server.Address
write-host "START Timer Service on server $servername" -fore yellow
(Get-WmiObject Win32_Service -filter "name='SPTimerV4'" -ComputerName $servername).startservice() | Out-Null
}


Comments (9)

  1. Thanks Nice to using it..

  2. Melvin says:

    Is there a script version for WSS 3.0 ???

  3. PrasadWT says:

    Hi Thomas, Your solution perfectly worked for me.I was facing this issue in my staging environment and after several IISReset and sp timer service restarts my wsp did not add into the solution store.I took your code and ran , and it worked like a charm.
    Thanks very much for your code.

  4. Anonymous says:

    Buenas, En este post queríamos haceros llegar este script para limpiar la cache de SharePoint. Este tipo

  5. PBN says:

    Hey Thanks Mate, this script really help me out.
    Could some one explain why this issue occurs

  6. Ana says:

    Thank you so much! This helped me out when trying to clear the Cache.ini file in a Windows Server 2008 R2 running Microsoft Sharepoint 2013

  7. Jason Warren says:

    Thank you for this code, it’s super useful. There’s no need to load the SharePoint snapin, there’s no SharePoint cmdlets used in the script. Thanks!

  8. Anonymous says:

    thanks i really needed this script

  9. Anonymous says:

    thanks i really needed this script

Skip to main content