Privacy Info

Saturday, October 8, 2022

Sitecore PowerShell Script - Item Cleaner


We have received request for deleting old items in Sitecore where items are modified before certain days (e.g. before 90 days) inside item folder. We checked in Sitecore content tree and found out the list for deleting items is huge. It would be time consuming task to delete each and every item manually. To solve this problem we have written PowerShell script which will identify items modified before certain days and delete them.

Script

function RemoveOldItems($folderPath)
{
		$oldDays =-90 # Items created before 90 days
		$folderName = $folderPath.split("/")[-1]   
		$items = Get-ChildItem -Path $folderPath -Recurse | Where-Object { $_.__Updated -lt [datetime]::Now.AddDays($oldDays) } 

		Write-Host $folderName "Delete Items Count: " $items.Count

		ForEach ($item in $items) 
		{
			Write-Host "Item ID:" $item.ID "Item Name:" $item.Name "Item Modified Date:" $item.__Updated "deleted"
			$item | Remove-Item     
		}
}

RemoveOldItems "master:/sitecore/content/Home/TestFolder"


You need to run above script in PowerShell window and it will delete items in specified folder where items are modified before certain days. Also you need to run the script on web database or publish the folder for changes to get reflected in web database.

Hope you find this information useful.😀



No comments:

Post a Comment

Sitecore PowerShell Script - Remove item from Workflow

We came across scenario where we need to remove multiple items from workflow. If items would be less then there would not be any issue but i...