Unloading a Component Definition


This is a follow up question to the recent question I had regarding converting components to groups.

Once I’ve successfully loaded the component and then inserted it into my container group and exploded it I no longer have use for the component definition and would like to completely remove it from the model.

I’ve tried to utilize both purge_unused and remove to accomplish this but both of these methods throws errors. I am basically passing these methods the original component definition that I loaded (after exploding the instance).



The error I’m getting for the remove method is:

Error: #<NoMethodError: undefined method `remove’ for #Sketchup::DefinitionList:0x0000000ebcd3c0>


This feature is only available for SketchUp 2018. For users of older versions, you better hope (or remind) the user to purge the model from time to time. If you purge unused definitions through a script you would also purge those that are not related to your extension and that the user might want to keep.


That’s what I was afraid of, I guess this is not going to work.

So in previous versions of SketchUp (2015, 2016 and 2017) there is no way to remove a specific component definition from the model other than to purge all unused components which as you suggest I would rather not do.


Correct. The kludgy workaround to prevent purging other definitions was to create a temp group and insert an instance of the other unused definitions - then perform a purge. (Not - pretty, I know. The #remove method should have been added a long time ago.)


I think the component definition is also removed if you delete all of its content. SketchUp doesn’t allow empty definitions.


And if Ruby code still has a reference to the definition after removing its contained entities? Does it immediately become an invalid reference (#<DeletedEntity>) to which you cannot add entities anymore, or does SketchUp purge it as soon as there is no reference any more?


I think the entity is truly deleted and the reference become a DeletedEntity. Haven’t poked at that part of SketchUp is some time though.


What would be the easiest way to delete all of the entities of the definition?

This couple of lines of code does not seem to work:

ents = data_compdef.entities
ents.erase_entities ents

This could be worth a shot.


Try definition.entities.erase_entities(definition.entities.to_a)

I think you have to pass an Array to erase_entities.

Also I changed the name data_compdef to a more generic name, and would recommend to have this extracted to a generic erase_definition method. Extracting things to separate methods with celar names, even if it’s just one line of code, can make it much easier to read, understand and maintain the code in the future.


Buzzinga! That works, and it works remarkably well.

Thank-you once again.

I would have never thought of trying this route.


How about simply …




Why didn’t I think of that :open_mouth:


On a tangential note, erasing groups or components with
is (for some mysterious reason) much, much faster on heavy geometry than using


Group.erase! may be shared with Edge.erase! and Face.erase! and do totally unnecessary checks to see if the entity in question was binding other entities that needs to be erased too, I don’t know. That should however be implemented at a deeper level so it’s not super likely.