Last active
February 6, 2016 16:50
-
-
Save AdamNaj/a8b1a0eedd2c5ebaf685 to your computer and use it in GitHub Desktop.
Remove unused media items
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
#Include all paths you do not want to analyse below | |
$protectedPaths = @( | |
"/sitecore/media library/System/", | |
"/sitecore/media library/Experience Explorer" | |
"/sitecore/media library/Images/Social" | |
); | |
#Include all item templates you want to ignore in the array below | |
$protectedTemplates = @( | |
[Sitecore.TemplateIDs]::MediaFolder | |
); | |
$itemsToDelete = | |
Get-ChildItem -Path "master:\sitecore\media library" -Recurse | | |
# filter out items of templates you do not want to touch | |
Where-Object { $_.TemplateID -notin $protectedTemplates } | | |
# do not allow items in the protected paths | |
Where-Object { | |
$item = $_; | |
$protected = $protectedPaths | Where-Object { ($item.Paths.Path) -match $_ }; | |
$protected.Count -lt 1; | |
} | | |
# and only items that are not used | |
Where-Object { [Sitecore.Globals]::LinkDatabase.GetReferrerCount($_) -eq 0 } | |
#List the items | |
$itemsToDelete | ft ProviderPath | |
#If the list above looks like what you want to delete you can uncomment the following line | |
#$itemsToDelete | Remove-Item | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sweet!