DEV Community

Cover image for Sitecore PowerShell Script to delete unused items in the Media Library
Daniel Gomez
Daniel Gomez

Posted on

4 1 1 1

Sitecore PowerShell Script to delete unused items in the Media Library

Hi! In this blog we will find a Sitecore PowerShell script to be able to eliminate multimedia elements that are not being used.

The general idea is to analyze the items or children of a specific path and verify that those items don't have any references. If the item has no reference, then it will be deleted.

Sitecore PowerShell Script:



$mediaLibraryRootPath = "master:/sitecore/media library"

# 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 if they have references. If the item has no reference, the media item is saved locally and then deleted.
function Get-MediaItemWithNoReference {

    $items = Get-ChildItem -Path $mediaLibraryRootPath -Recurse | 
        Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder }

    $i = 0
    foreach($item in $items) {
        if(!(HasReference($item))) {
            $i = $i + 1
            Write-Output "$($i). $($item.ItemPath)"
            $item | Remove-Item 
        }
    }
}

# Run the script.
Write-Output "Items to delete:"
Get-MediaItemWithNoReference



Enter fullscreen mode Exit fullscreen mode

Example:

This is an example of an unused image that can be removed using the script:

Run the PowerShell script

Thanks for reading!

I hope this script has been useful to you. If you have any questions or ideas in mind, it'll be a pleasure to be able to be in communication with you, and together exchange knowledge with each other.

X / LinkedIn - esdanielgomez.com

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay