BUG - Grasshopper Blocks - Cannot Delete - Trapped In Memory?

Okay after much more digging I realized this is not a bug but the way the the InstanceDefinition Table works in Rhino apparently. Nothing gets “deleted” but rather tagged as it being deleted so a “version of itself” still exists in the table despite it having no Name or info once it is “deleted”.

This was very confusing to me but I think it’s all working now. Sharing here in case anyone stumbles across this in the future.

This post solved it for me:

Example Code That Doesn’t Crash (Check If The InstanceDefinition Is Not “Deleted” First):

import Rhino
import scriptcontext as sc


def search_and_clean_block_instances():
    block_instances = sc.doc.InstanceDefinitions if hasattr(sc.doc, 'InstanceDefinitions') else []

    if block_instances:
        for instance in block_instances:
            if not instance.IsDeleted:
                # Print instance definition
                Rhino.RhinoApp.WriteLine(f"InstanceDefinition: {instance.Name} ({instance.Index})")
            else:
                Rhino.RhinoApp.WriteLine("Instance Exists In Table")
    else:
        Rhino.RhinoApp.WriteLine("No Blocks Exist")


if __name__ == "__main__":
    search_and_clean_block_instances()