Blog/Media Library ·9 Jul 2014 ·

How to query for unused assets in media library in sitecore using powershell

If assets in media library in sitecore are stored in the database after some time your db can grow to considerable size. I created the powershell script, which can query for unused resources larger than 1MB.

A too big database generates a lot of problems. It is increasingly difficult to backup the database because the backup process, compression and upload to - for example - FTP may take several hours. If you have additional specific requirements like to store backups from the last 7 days, last 6 Fridays, 4 last first Fridays of the month, then you need a lot of storage space.

The PowerShell script to find unused media items over 1MB

Below is a PowerShell script that you can use to search media library for unused items larger than 1MB. You can then ask the 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.Globals]::LinkDatabase.GetReferers($_).Length -eq 0} 
    | sort-object { [System.Int32]::Parse($_.Size) } 
    | format-table {$_.Paths.Path, $_.Size, [Sitecore.Globals]::LinkDatabase.GetReferers($_).Length}

Running it from PowerShell Extensions or Sitecore Rocks

This script will only work if you are using PowerShell Extensions (inside Sitecore desktop). If you are using PowerShell 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.

Written by

Bartłomiej Mucha

Sitecore MVP Technology 2019

I started programming on my first computer, the Commodore 64, in 2000, and have been implementing solutions in Sitecore since 2011.

Connect on LinkedIn →

Related reading