Archive for January 29, 2015

Shrink CrawlStore Database

A useful script for running on your dev\uat environments to free valuable disk space. The crawl store database can grow quite significantly over time.

First of all, the PowerShell script:

# Load the SharePoint assembly - change the version to 15.0.0.0 for SharePoint 2013
Add-Type -AssemblyName "Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
 
# Reset the content index
(Get-SPEnterpriseSearchServiceApplication).Reset($true,$true)
 
# Get the database server for the default content service
$dbServer = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.DefaultDatabaseInstance.NormalizedDataSource
 
# Get the SQL Management Object server for the content service retrieved above
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$SMOserver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist $dbServer
 
# Get the search service application instance
$searchServiceApp = (Get-SPEnterpriseSearchServiceApplication)
 
# Iterate the crawl stores and for each database, shrink it in size by 20%
foreach($crawlStore in $searchServiceApp.CrawlStores)
{		
	$dbName = $crawlStore.Name	
	$db = $SMOserver.Databases[$dbName]
	if ($db -ne $null)  
	 {  
		$db.Shrink(20, [Microsoft.SqlServer.Management.Smo.ShrinkMethod]'TruncateOnly')
		Write-Host "$dbName has been shrunk"
	 }
	 else
	 {
		Write-Host "$dbName does not exist"
	 }
}

Comments in the script should make this one clear to understand, happy coding!

Follow

Get every new post delivered to your Inbox

Join other followers: