Friday, October 24, 2014

Sitecore PowerShell Extensions Tip - Delete Unused Media Items

Sitecore PowerShell Extensions

Delete unused media items older than 30 days.


<#
.SYNOPSIS
Moves items to the recycle bin which are more than 30 days old and have no references.
.NOTES
Michael West
#>
filter Skip-MissingReference {
$linkDb = [Sitecore.Globals]::LinkDatabase
if($linkDb.GetReferrerCount($_) -eq 0) {
$_
}
}
$date = [datetime]::Today.AddDays(-30)
$items = Get-ChildItem -Path "master:\sitecore\media library" -Recurse |
Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder } |
Where-Object { $_.__Owner -ne "sitecore\admin" -and $_.__Updated -lt $date } |
Skip-MissingReference
if($items) {
Write-Log "Removing $($items.Length) item(s) older than 30 days."
$items | Remove-Item
}

1 comment:

  1. Sir I want to remove the data from recycle bin. The code which you have given above is not working fine for me. The scenario is if i delete the image from recycle bin then that data has to get deleted from sitecore blob table.

    ReplyDelete