Skip to main content

Hello, programming world!
Knowledge shared is knowledge gained.

DISCLAIMER: Using the information provided here do so at your own risk. Blog may contain errors of grammar and spelling. Sorry about that. I am writing this blog in English in order to practice it. If you find a mistake I will be grateful for the information. Site is using cookies. By using site you agree to use cookies.

How to query for unused assets in media library in sitecore using power shell

Too big database generates a lot of problems. It is increasingly difficult to backup the database, because backup process, compression an upload to eg FTP, may take several hours. If you have additional specific requirements eg. to store backups from the last 7 days, last 6 fridays, 4 last first fridays of the month, then you need a lot of space for their storage.

Below is a power shell script that you can use to search media library for unused items larger than 1MB. You can then ask client to delete them.

cd "media library"

get-childitem -recurse 
    | where-object { $_.Size -ne $null -and [System.Int32]::Parse($_.Size) -ge [System.Int32]::Parse(1048576) -and [Sitecore.Glo
bals]::LinkDatabase.GetReferers($_).Length -eq 0} 
    | sort-object { [System.Int32]::Parse($_.Size) } 
    | format-table {$_.Paths.Path, $_.Size, [Sitecore.Globals]::LinkDatabase.G

A small note. This script will only work if you are using Power Shell Extensions (inside Sitecore desktop). If you are using Power Shell from inside Sitecore Rocks you need to use cmdlets. As far as I know, cmdlet for getting related items will be available in version 2.7.

comments powered by Disqus