Delete groups that intersects with another group

Hi, I’m new to SketchUp and also Ruby, so I’m trying to delete all objects that somehow intersects with another object. In this example I’ve got many objects that are group, and one single object with whom I trimmed this groups to obtain what I have in center. Now, I just need to delete objects that are in intersection with this group. This is what I tried to do, but without succes.

trim operation + creation of new_group_array
[new_group_array =
group_array.each {|group|
new_group_array.push((external_group.trim(group)).explode)
}

    p "new_group_array.length: #{new_group_array.length}"
    p "external_group: #{external_group}"
    external_group.explode
    
    #just delete all tiles
    new_group_array.each {|group|
      if external_group.bounds.intersect(group.bounds)
          group.erase!
              end}

I also tried to explode all objects and to do that, but I get #<TypeError: reference to deleted DrawingElement>

screen3

On the last screen is what I expect to get in final.
Thanks in advance for your help !

A post was merged into an existing topic: Delete groups that intersects with another group