SitecorePowerShell/Console

Remove-Item can target incorrect item if two items have same path

Closed this issue · 2 comments

Expected Behavior

I have two items with the same path (same item name). In this case, "Component". The first one in the tree has children, the second does not.

  • Page
    • Components
      • Component (ID is 1)
        • Child
      • Component (ID is 2)

I want to delete the second one, and I have that item referenced via ID to avoid any issues with paths:

$item = Get-Item -Path "master:" -ID "2"
$children = Get-ChildItem -Path "master:" -ID $item.ID
if($children.Length -eq 0) {
  Write-Host "Deleting $($item.ID) $($item.Paths.FullPath) version $($item.Version.Number) with 0 children."
  $item | Remove-Item -Confirm:$true
} else {
  Write-Host "Item has children. Not deleting: $($item.Paths.FullPath) version $($item.Version.Number)."
}

The second Component item should be deleted.

Actual Behavior

My console prints this:

Deleting {1} /sitecore/content/Page/Components/Component version 1 with 0 children.

But the confirmation message for the Remove-Item command says this, implying that it's going to delete the item that I am not intending to delete:

The item at Sitecore::master:\content\Home\Page\Components\Component has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?

Taking a guess here, there might be a limitation in the PowerShell API such that it accepts a path rather than some other unique value.

protected override void RemoveItem(string path, bool recurse)

This may be the first time I've tried to actually use a script to delete duplicate items (wrote a report to detect them).

Try using this as a workaround:

$item.Delete()

That works -- thank you.