# Deleting all faces (and connected edges) with a specific material. (Error: TypeError: reference to deleted Entity)

**URL:** <https://forums.sketchup.com/t/deleting-all-faces-and-connected-edges-with-a-specific-material-error-typeerror-reference-to-deleted-entity/175689>\
**Category:** Ruby API\
**Created:** [September 16, 2021, 5:44pm UTC](https://forums.sketchup.com/t/deleting-all-faces-and-connected-edges-with-a-specific-material-error-typeerror-reference-to-deleted-entity/175689 "2021-09-16T17:44:05Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [September 16, 2021, 7:44pm UTC](https://forums.sketchup.com/t/deleting-all-faces-and-connected-edges-with-a-specific-material-error-typeerror-reference-to-deleted-entity/175689/6 "2021-09-16T19:44:30Z")

</div>

We seem to be forever explaining this fundamental rule …

> [@Why this error and how to correct it?](https://forums.sketchup.com/t/why-this-error-and-how-to-correct-it/144303/22):
>
> The reason this happens is because you are breaking a fundamental rule of collections. The rule is: Do not modify the collection while you are iterating it. Instead, save up an array of items (faces) to be deleted, and then use the bulk entities delete method: [Sketchup::Entities#erase\_entities](https://ruby.sketchup.com/Sketchup/Entities.html#erase_entities-instance_method) edges\_to\_erase = [] grp.entities.grep(Sketchup::Edge) do |e| edges\_to\_erase \<\< e if e.faces.size != 2 end grp.entities.erase\_entities(edg…

…

> [@Why this error and how to correct it?](https://forums.sketchup.com/t/why-this-error-and-how-to-correct-it/144303/26):
>
> Generally yes. However, in this case it is more complex because it is one of the [Entities](https://ruby.sketchup.com/Sketchup/Entities.html) collections and the calls to [edge.erase!](https://ruby.sketchup.com/Sketchup/Drawingelement.html#erase!-instance_method) cause the SketchUp engine to also erase faces if the deleted edge was on the outer loop of a face. Look back and you’ll see the error was that calls to [edge.faces](https://ruby.sketchup.com/Sketchup/Edge.html#faces-instance_method) returned empty arrays that return nil for [.first](https://ruby-doc.org/core-2.5.5/Array.html#method-i-first) and [.last](https://ruby-doc.org/core-2.5.5/Array.html#method-i-last). So also a conditional like next if edge.faces.empty? can be used to avoid this error. But is is still safer to collect the edges to be erased …

So I opened an API Knowledge Issue ~~in~~  ~~June~~ last Jan …

> <https://github.com/SketchUp/api-issue-tracker/issues/592>
>
> \### SketchUp Ruby API Documentation Issue
> 
> \*\*Write article on accessing API co…llections use, tips and safely deleting members from them.\*\*
> 
> \_\*\*Reissued\*\* from part (1) of \[Issue 525 : Pages#each documentation and code example improvement\](https://github.com/SketchUp/api-issue-tracker/issues/525).\_
> \_Part (2) was applied to fix the code snippet and then that issue was closed.\_
> 
> \> \*\*(1)\*\* Please add a note warning that individual scene pages should not be erased within a block passed to this method, and that an array copy should be used instead ...
> \> \`\`\`ruby
> \> pages.to\_a.each { |page| pages.erase(page) }
> \> \`\`\`
> \> \_REF:\_ https://forums.sketchup.com/t/erase-all-scenes-pages/134922
> \> 
> 
> ... in which \[\_Eneroth asked:\_\](https://github.com/SketchUp/api-issue-tracker/issues/525#issuecomment-689383498)
> \> \_Regarding .1, that applies to more or less every SketchUp API class implementing #each, right?\_
> 
> \*\*I replied thus ...\*\*
> 
> If you look through the referenced forum thread, there is a lot of good "meat" there for a good start on a general tutorial page for SketchUp API ("thinly wrapped" C++) collections. And that is what we we really talking about in that thread, although several also wanted to bring in pure Ruby collections like Arrays. And yes, it applies to both, but the API docs don't document Ruby core classes.
> 
> So with regard to API collection classes, \*\*generally\*\*, the advice would apply to any C++ collection exposed to the Ruby API, with any iterator (\`#each\` etc.) that also either has: a member deletion method (\`delete\`, \`erase\`, \`remove\`, etc.) for the collection, or members that have an \`#erase!\` or \`delete\` method.
> 
> These are a good list of methods that should not be called from within their own \`#each\` iterator block ...
> \[\`Sketchup::AttributeDictionaries#delete\`\](https://ruby.sketchup.com/Sketchup/AttributeDictionaries.html#delete-instance\_method)
> \[\`Sketchup::AttributeDictionary#delete\_key\`\](https://ruby.sketchup.com/Sketchup/AttributeDictionary.html#delete\_key-instance\_method)
> \[\`Sketchup::Set#delete\`\](https://ruby.sketchup.com/Sketchup/Set.html#delete-instance\_method)
> 
> \[\`Sketchup::Entities#erase\_entities\`\](https://ruby.sketchup.com/Sketchup/Entities.html#erase\_entities-instance\_method)
> \[\`Sketchup::Pages.erase\`\](https://ruby.sketchup.com/Sketchup/Pages.html#erase-instance\_method)
> 
> \[\`Sketchup::DefinitionList#remove\`\](https://ruby.sketchup.com/Sketchup/DefinitionList.html#remove-instance\_method)
> \[\`Sketchup::Layers#remove\`\](https://ruby.sketchup.com/Sketchup/Layers.html#remove-instance\_method)
> \[\`Sketchup::Materials#remove\`\](https://ruby.sketchup.com/Sketchup/Materials.html#remove-instance\_method)
> 
> Tests on using \`remove\` within a \`selection#each\` block fail. (Only 1 object is removed.)
> \[\`Sketchup::Selection#remove\`\](https://ruby.sketchup.com/Sketchup/Selection.html#remove-instance\_method)
> 
> \[\`Layout::AutoTextDefinitions#remove\`\](https://ruby.sketchup.com/Layout/AutoTextDefinitions.html#remove-instance\_method)
> \[\`Layout::Layers#remove\`\](https://ruby.sketchup.com/Layout/Layers.html#remove-instance\_method)
> \[\`Layout::Pages#remove\`\](https://ruby.sketchup.com/Layout/Pages.html#remove-instance\_method)
> 
> \----
> 
> I also tested \`Entities#each {|e| e.erase! }\` and it seemed to work okay. But only in simple tests of under 40 items.
> I'd think this also should apply to the general rule. Save up the items to be erased, and use the bulk \`#erase\_entities\` method afterward.
> 
> \----
> 
> \*\*Also ...\*\*
> 
> So ... with regard to any specific individual warning note(s). Where to put them ?
> 
> a) A the top of every class that has \`#each\` and a deletion method ?
> b) In each of these classes' deletion method docstrings ?
> 
> ... and ...
> 
> Do we put a "Deleting Safely from Collections" section in a "Files" page ... and just link to this section from all the methods ?
> ... rather than violate the DRY rule and repeat the same warning all those places ?

---

_[View the full topic](https://forums.sketchup.com/t/deleting-all-faces-and-connected-edges-with-a-specific-material-error-typeerror-reference-to-deleted-entity/175689)._
