Help Deleting Objects

so im having problems trying to delete objects with python. sorry for this being a noob question. so i found this other thread that someone posted an example script. i copied but it had like 8 errors when i tried to use it. then i corrected it to this.


import bpy
obj = bpy.data.objects['Plane']
scene = bpy.data.scenes[0]
bpy.ops.object.select_all(action='DESELECT')
 
 
obj.select = True
scene.objects.active = obj
 
 
for i in range(len(obj.material_slots)):
obj.active_material_index = i - 1
bpy.ops.object.material_slot_remove()
 
bpy.ops.object.parent_clear(type='CLEAR')
bpy.data.objects.remove(obj) 

when i do that it says the plane must have 0 objects to be removed, but it has 1. i have no idea what that means. could someone help me please?

By the way, use code and /code in square brackets …!!! for the layout!
Do not understand too,
but this worked


import bpy
def delThisObj(obj):
    bpy.ops.object.select_name(name=obj.name)
    bpy.context.scene.objects.active = obj
    print("deleting ",obj.name)
    bpy.ops.object.delete() 
obj = bpy.data.objects['Plane']
 
for i in range(len(obj.material_slots)):
 obj.active_material_index = i - 1
 bpy.ops.object.material_slot_remove()
 
print(obj)
delThisObj(obj)

Here is another way to remove an object from the scene without altering the current selection.


import bpy
 
obj = bpy.data.objects['Cube']
bpy.data.scenes[0].objects.unlink(obj)
bpy.data.objects.remove(obj)

You do have to specify the scene index, however. If the active scene is not scene #0 you will have to alter that index.

Thanks Atom, exactly what I was looking for!