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 for SharePoint 2013
Add-Type -AssemblyName "Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
# Reset the content index
# 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"
		Write-Host "$dbName does not exist"

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

This entry was posted in SharePoint, SQL and tagged , , . Bookmark the permalink.
0 0 votes
Article Rating
Notify of

Solve the maths problem shown below before posting: *

Inline Feedbacks
View all comments