How to delete objects from view

Hi,

I’m creating several object in the view with names like:
MyItem1
MyItem2
MyItem3
etc.

When finished I want to delete all the generated objects.
So how can I delete all object that start with the text “MyItem”?


import bpy


def del_myitems(scene):
    #scene = context.scene
    myitems = [o for o in scene.objects if o.name.startswith("MyItem")]
    for ob in myitems:
        ob.animation_data_clear()
        ob.use_fake_user = False
        scene.objects.unlink(ob)
        if ob.users == 0:
            bpy.data.objects.remove(ob)
            
#test call
del_myitems(bpy.context.scene)

EDIT prob don’t need the animation_data clear, copied from a script I’m working on.

Seems to work perfect. Thanks again!