Managing deprecated content in large-scale applications with PowerShell.
Welcome to another blog of series Sitecore Loves PowerShell
When working with large-scale applications, we often encounter deprecated content that negatively impacts the CM node. This makes content authoring and management a real headache.
One of my content authors approached me and said, “Kunal, I’ve identified 511 Sitecore nodes we can deprecate since we’re not using them.” It sounded simple enough, but after analysing the request, I realized it wasn’t just 511 Sitecore nodes—it was 511 Sitecore client tenant sites. This included 3,000 client pages and 13,423 data sources and content items.
It was a massive task, and I knew only PSE could help me tackle it. I quickly started writing a script to read the nodes from Excel, like this.
Then, I began deleting the nodes recursively, like this.
Easy, right? Not quite. The real pain began when I saw how long it was taking. On my local machine, it took over 50 minutes to delete everything. There was no way I could run this script in production.
I needed to speed things up, and I remembered a piece of code I had used before to accelerate Sitecore operations.
When performing operations in Sitecore with PowerShell, it always triggers associated events like indexing the database, save events, and internal updates, which take a lot of time. But if you’re confident you don’t need those events to run, you can use “BulkUpdateContext.”
Trust me, this significantly improves performance. The code without BulkUpdateContext took over 50 minutes, but with BulkUpdateContext, I reduced it to just 8 minutes.
you can have my script snippet below.
For more updates like this, subscribe to my newsletter and follow me on LinkedIn.
Write-Host "Started the execution"
# Import the CSV file.
$resultSet = Import-Csv "$($SitecoreDataFolder)\packages\ClientMicrositeTenant.csv"
# Check if there are rows in the CSV file.
$rowCount = ($resultSet | Measure-Object).Count
if ($rowCount -le 0) {
Remove-Item "$($SitecoreDataFolder)\packages\ClientMicrositeTenant.csv"
exit
}
Write-Host "sitecore item deletion started "
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
foreach ($row in $resultSet) {
Write-Host "$($row.ItemName)"
Get-Item -Path "web:$($row.SitecorePath)" | Remove-Item -Recurse
}
}
Subscribe to my newsletter
Read articles from Kunal Soni directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by