1

The CMS has many duplicate/redundant items and I would like to delete them.
When I select an item (say Item-A) and check Navigate > Links, there are no items that refer Item-A.

But, somewhere else, Item A is being referred in the rendering datasource field like this: query:$site/Data/Banners

When items are not referenced directly and instead using a query, then even when I delete Item-A, it does not show any alert that it is being used.

In such scenario, is there a way to find out unused items.

Using Sitecore 10.1.3 with SXA.

1
  • Rebuild your link database, then you can run the PowerShell command to find reference items. Commented Dec 6, 2023 at 18:31

2 Answers 2

1

This code helps to find the related item.

var unusedItems = Sitecore.Context.Database.GetItems().Where(item =>
    !Sitecore.Globals.LinkDatabase.GetReferrers(item).Any());

and if you are using SPE, below code will help

# Get all items in the content tree
$allItems = Get-Item -Path "master:" -Language *

# Get the reference map
$refMap = Get-ItemReferrerMap -Item $allItems

# Find items not referenced by any other item
$unusedItems = $allItems | Where-Object { -not $refMap.ContainsKey($_.ID) }

# Output the unused items
$unusedItems | Format-Table -Property Name, ItemPath
1

I do not think there is a readymade solution available for this. These tokens $site is evaluated at runtime and hence you do not see it in references.

Since you already have content ready, you can create a handler when item is deleted item:deleting event and look for such cases under content node. This would be a complex code considering multiple scenarios like checking presentation and evaluating those tokens.

     public class OnItemDeletingValidator
       {
        public void OnItemDeleting(object sender, EventArgs args)
        {
         Item item = Event.ExtractParameter(args, 0) as Item;
         // Your logic to iterate through content items and find if this item is used under Page presentation
        }
       }

And register this event using a config patch file.

Here is an article with details - https://briancaos.wordpress.com/2020/01/03/sitecore-use-itemdeleting-event-to-avoid-deletion-of-items/

Hope this helps !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.