How to remove selected vertexes from all vertex group, except locked

How to remove selected vertexes from all vertex group, except locked? Unfortunately, “Remove from all groups” removes the selected vertices from the blocked ones as well. All the methods of removing vertices from vertex groups that I have found are removed either from only one selected group, or from all at once.
blender_2022-06-17_21-37-18

There is an option to delete all vertex groups that are unlocked from a selected object.

I need to remove vertices from unlocked vertex groups, but not the groups themselves.

To my knowledge, you cannot clear nor move a vertex group column order.

https://docs.blender.org/api/current/bpy.types.VertexGroups.html#bpy.types.VertexGroups.remove

https://docs.blender.org/api/current/bpy.types.VertexGroup.html#bpy.types.VertexGroup.remove

https://docs.blender.org/api/current/bpy.types.VertexGroup.html#bpy.types.VertexGroup.index


What you can do however, is delete all unlocked vertex groups, then recreate them from scratch, using their saved names as the new vertex group name(s). Unfortunately, as I mentioned above, there doesn’t seem to be a way to set a vertex group index column, meaning they’ll be out of order if you run the code, however, that might be acceptable, not sure.

import bpy

VERTEX_GROUPS = []

for g in bpy.context.object.vertex_groups:
    if g.lock_weight == False:
        VERTEX_GROUPS.append(str(g.name))
        bpy.context.object.vertex_groups.remove(g)
        
for v in VERTEX_GROUPS:
    n = bpy.context.object.vertex_groups.new(name=str(v))
    n.lock_weight = True

The script also requires the locking of the new vertex groups, otherwise Blender will infinitely create and delete vertex groups in an endless loop.