Delete Bone keyframes from F-curves

I have created a function to delete object keyframes in range. Now, I’m creating the same functionality for a bone.

The function receives the bone, the type “Loc/Rot/Scale” and the frame range.

The loop read the curves and select only the Loc/Rot/Scale type and the bone using the name of the datapath.

This is the function:

import bpy


#---------------------------------------
# Clear animation frames from Bone
#
# myBone: Bone to clear
# typ: Loc/Rot/Scale
#---------------------------------------
def clearBoneAnimation(myBone,typ,fromFrame,toFrame):
     # Get current action name      
    ob = bpy.context.object
    try:
        action_name = ob.animation_data.action.name
    except:
        return {'FINISHED'}
    
    if (action_name != None):
        a = bpy.data.actions.get(action_name)    
        # Loop all f-curves
        for curve in a.fcurves:
            if curve.lock == False and curve.data_path[-len(typ):] == typ:
                if "[\"" + myBone.name in curve.data_path:
                    keyframePoints = curve.keyframe_points
                    for keyframe in keyframePoints:
                        x = keyframe.co[0] # get frame number
                        
                        # only delete frame range
                        if x >= fromFrame and x <= toFrame:
                            print("Removing keyframe")
                            #keyframePoints.remove(keyframe)  <<<<< HOW DELETE?
                            
                            
# Call funcion   
print("========= Running ==================")                     
clearBoneAnimation(bpy.context.active_bone,"location",5,45)

The problem it’s the remove itself. I have tested several options but nothing works.

How can I delete the keyframe (X/Y/Z)?

Thanks.

#batFinger

Great!

I have tested a lot of things before your code but yours works perfect.

Thanks !

Looking to do something similar and ran into this. What was the resolution?