Today I am going to share a PowerShell script that is used to delete all the descendant items of a parent which are based on a particular template.
When deleting a Sitecore item with many descendants sometimes the operation times out due to which some of the items do not get deleted.
This PowerShell script will inform you line by line what items are deleted successfully.
$items = Get-ChildItem -Path "/sitecore/content/Home" | Where-Object { $_.TemplateID -eq "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}" }
foreach ($item in $items)
{
Write-Host "Deleting Item" $item.Name
$item | Remove-Item
}
If you want to permanently remove an item without sending those items to recycle bin then you need to add “-Permanently” to the Remove-Item command. Below is the PowerShell script for the same.
$items = Get-ChildItem -Path "/sitecore/content/Home" | Where-Object { $_.TemplateID -eq "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}" }
foreach ($item in $items)
{
Write-Host "Deleting Item" $item.Name
$item | Remove-Item -Permanently
}
References
Sitecore PowerShell - https://doc.sitecorepowershell.com/
Sitecore PowerShell GitHub - https://github.com/SitecorePowerShell/Console