Skip to content

Instantly share code, notes, and snippets.

@AlexKasaku
Created August 20, 2015 13:45
Show Gist options
  • Save AlexKasaku/2ce1650337d1b6a928ad to your computer and use it in GitHub Desktop.
Save AlexKasaku/2ce1650337d1b6a928ad to your computer and use it in GitHub Desktop.
Remove all media items that are not referenced.
<#
.SYNOPSIS
Removes all media items that are not linked to other items.
#>
# HasReference determines if the specified item is referenced by any other item.
function HasReference {
param(
$Item
)
$linkDb = [Sitecore.Globals]::LinkDatabase
$linkDb.GetReferrerCount($Item) -gt 0
}
<#
Get-MediaItemWithNoReference gets all the items in the media library
and checks to see if they have references. Each item that does not
have a reference is passed down the PowerShell pipeline.
#>
function Get-MediaItemWithNoReference {
$items = Get-ChildItem -Path "master:\sitecore\media library" -Recurse |
Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder } |
Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::Folder }
foreach($item in $items) {
if(!(HasReference($item))) {
$item
}
}
}
Get-MediaItemWithNoReference | Remove-Item -Recurse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment