I need to delete all the items present in the list view on the click of the "delete all" action button.

enter image description here I am running the script -

foreach($selectedItem in $allData) {    
   $item = Get-Item -Path "master:" -ID $selectedItem.ID
   $item | Remove-Item

But it deletes only the first item in the list. Maybe it is throwing some error in $allData.Remove($selectedItem). When I try to execute it in ISE with test data in $allData, it throws below error:

Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."

Please suggest how to resolve this?

  • why do you want to remove item from $allData? are you doing anything with it? – Mahendra Shekhawat Oct 15 '19 at 7:45
  • @MahendraShekhawat Based on previous question at sitecore.stackexchange.com/q/22542/605, it seems that he wants to update the listview – Hishaam Namooya Oct 15 '19 at 8:05
  • @Surya I went on with Mahendra Shekhawat's comment and didn't update $allData. Instead, showed a popup that all the items are deleted and closed the window. – NewbieHere Oct 18 '19 at 10:17

You can try for loop instead of foreach loop if you want to update collection within the loop

Example -

for($index = $allData.Count-1; $index -ge 0; $index--) {
   $item = Get-Item -Path "master:" -ID $allData[$index].ID
   $item | Remove-Item

If $allData is a collection of items then below script is enough to remove items and update $allData collection -

for($index = $allData.Count-1; $index -ge 0; $index--) {
   $allData[$index] | Remove-Item

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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