Skip to content

Instantly share code, notes, and snippets.

@AdamNaj
Last active February 6, 2016 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamNaj/a8b1a0eedd2c5ebaf685 to your computer and use it in GitHub Desktop.
Save AdamNaj/a8b1a0eedd2c5ebaf685 to your computer and use it in GitHub Desktop.
Remove unused media items
#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
@michaellwest
Copy link

Sweet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment