1

I am trying to create a powershell script using which i can delete bulk number of items using powershell along with removing the references of that item.

So basically would like to achieve the same feature what sitecore does if we right click and delete the item from sitecore cms, it removes all the references of parent and child items.

Currently I am using the below script which is working fine for a single item i.e. deletes the item and removes the references of this item. But if suppose I have child item and that child item is referenced anywhere, this script simply deletes the parent item with child item but not removing the child item references.

function Remove-ItemLink {
param([Item]$item)

$linkDb = [Sitecore.Globals]::LinkDatabase

$links = Get-ItemReferrer -Item $item -ItemLink

foreach($link in $links) {
    $linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID 
    $itemField = $linkedItem.Fields[$link.SourceFieldID]
    $field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)

    $linkedItem.Editing.BeginEdit()
    $field.RemoveLink($link)
    $linkedItem.Editing.EndEdit()
}
}

$url = @("master:/sitecore/content/Home/ItemToDelete","master:/sitecore/content/Home/ItemToDelete/Item1","master:/sitecore/content/Home/ItemToDelete/Item1/Item1Child")
foreach($item in $url) {
  $itemsToDelete = Get-Item -Path "$item"
  Remove-ItemLink -Item $itemsToDelete
  Remove-Item -Path $itemsToDelete.Paths.Path
}

Any suggestions.

2 Answers 2

1

I checked the same script in my system and what I found is that the sequence of items you are passing is the Parent Item (i.e. ItemToDelete), then the first Child (i.e. ItemToDelete/Item1), and then the Second Child (i.e. ItemToDelete/Item1/Item1Child).

You should pass the child item first and then the parent item at the last.

So if you change the sequence like below then it will delete all the child and parent references and delete them.

$url = @("master:/sitecore/content/Home/Item1/Item2/Item3", "master:/sitecore/content/Home/Item1/Item2", "master:/sitecore/content/Home/Item1")

So your script will look like this.

function Remove-ItemLink {
param([Item]$item)

$linkDb = [Sitecore.Globals]::LinkDatabase

$links = Get-ItemReferrer -Item $item -ItemLink

foreach($link in $links) {
    $linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID 
    $itemField = $linkedItem.Fields[$link.SourceFieldID]
    $field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)

    $linkedItem.Editing.BeginEdit()
    $field.RemoveLink($link)
    $linkedItem.Editing.EndEdit()
}
}

$url = @("master:/sitecore/content/Home/Item1/Item2/Item3", "master:/sitecore/content/Home/Item1/Item2", "master:/sitecore/content/Home/Item1")
foreach($item in $url) {
  $itemsToDelete = Get-Item -Path "$item"
  Remove-ItemLink -Item $itemsToDelete
  Remove-Item -Path $itemsToDelete.Paths.Path
}

Hope this helps.

0

Your PowerShell script currently deletes items along with their references, but it doesn't handle child items references. To achieve this, you can modify the script as follows:

function Remove-ItemAndReferences {
 param(
     [Item]$item
 )

 # Recursively delete child items and their references
 foreach ($child in $item.Axes.GetDescendants()) {
     Remove-ItemAndReferences -Item $child
 }

 # Remove references to the current item
 Remove-ItemLink -Item $item

 # Delete the item
 $item | Remove-Item -Force
}

function Remove-ItemLink {
 param(
     [Item]$item
 )

 $linkDb = [Sitecore.Globals]::LinkDatabase
 $links = Get-ItemReferrer -Item $item -ItemLink

 foreach ($link in $links) {
     $linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID 
     $itemField = $linkedItem.Fields[$link.SourceFieldID]
     $field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)

     $linkedItem.Editing.BeginEdit()
     $field.RemoveLink($link)
     $linkedItem.Editing.EndEdit()
 }
}

# Paths of items to delete
$urls = @("master:/sitecore/content/Home/ItemToDelete",
       "master:/sitecore/content/Home/ItemToDelete/Item1",
       "master:/sitecore/content/Home/ItemToDelete/Item1/Item1Child")

foreach ($url in $urls) {
 $itemToDelete = Get-Item -Path $url
 Remove-ItemAndReferences -Item $itemToDelete
}

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.