How to remove modifier_001? syntax

Well, the following syntax removes the modifier’s subsurf.

bpy.ops.object.modifier_remove(modifier=“Subsurf”)

But this is only the one with the name subsurf. That is, duplicate modifiers with _001 and _002 cannot be deleted.
I checked the syntax of python and checked api, but the * wildcard does not seem to work.
How can python be able to remove modifiers with _001 and _002 in their names? As with other objects, are regular expressions not supported?

list the obj.modifiers, match the names and delete the matching modifiers

This is how I did in the past

    for mod in obj.modifiers:
        print(mod)
        if mod.name == "MYMODIFIER": # or if  "MYMODIFIER" in mod.name:
          bpy.ops.object.modifier_remove( modifier = mod.name )


Make sure that the object is active since the ops operate on the current object.

thanks. I tried it but it doesn’t seem to work.
Next we did modifier solidify, but 001 and 002 remain.
Although the specification of obj may be wrong with the addition, it is not clear.

obj = context.active_object
for mod in obj.modifiers:
print(mod)
if mod.name == “Solidify”:
bpy.ops.object.modifier_remove( modifier = mod.name )

You need to pass the actual name like (modifier = “Solidify_001”) etc
mod.name is literally is the name of the modifier not the type.

1 Like

Check the type if you want to remove all of the same type.

if mod.type == ‘SOLIDIFY’:
bpy.ops.object.modifer_remove(modifier = mod.name)

Or use string pattern matching:

if mod.name.startswith(‘Solidify’)

1 Like

Oh, it didn’t notice! Thank you for teaching me.
But as we will see later, we decided to deal with it in a way that might not be smart.
Thank you for the help.

Type specification and pattern matching feel like a good technique. Thank you for teaching me.
Unfortunately we couldn’t use it. Of course it is not that mistake.
I couldn’t put in a code that worked.
Thank you for the help.

Well, after all, I think that our code is not very clear.
The solution to this is this:

bpy.ops.object.modifier_remove(modifier=“Solidify”)
bpy.ops.object.modifier_remove(modifier=“Solidify.001”)

I just extend the numbers. 001,002,003,004,
I know, it is likely to be spaghetti code … =)