Remove item from list within a for each activity, "Collection was modified; enumeration operation may not execute"

How do I remove an item from a list by its value, while inside a for each loop?

Loop through a list of strings, if item starts with a certain string, remove it from the list

  • I am currently using list.Remove(“item”), is this the right way to delete by value?
  • I currently get error Collection was modified; enumeration operation may not execute when I run the workflow


This usually happens when you try to delete an item inside the for each loop. Error is thrown when you are modifying the collection that is being looped. To avoid this:

  1. Instead of For Each loop, use “Exist in Collection” activity to check if the item exist in the list and store the result in a boolean variable
  2. Using an If condition check with the boolean var = True then I’ve used an Invoke method to call the Remove method
  3. pass the Target Obj as the List var and pass parameter - string being searched for

Hope this was helpful, please mark as solution if this resolves your problem.


1 Like