Created
August 20, 2015 13:45
-
-
Save AlexKasaku/2ce1650337d1b6a928ad to your computer and use it in GitHub Desktop.
Remove all media items that are not referenced.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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