# delete bone constraint in python

I have a quick and dirty script to change the constraint settings for the bones of an armature:

import bpy

for bone in bpy.context.selected_pose_bones:
try:
bone.constraints[0].target_space='LOCAL'
bone.constraints[0].owner_space='LOCAL'
for cons in bone.constraints:
if cons.type == 'COPY_LOCATION':
WHAT GOES HERE TO DELETE THE CONSTRAINT?
except IndexError:
pass


As you can see I also want to delete any Copy Location constraints on the bones and can't figure out how. Manually deleting generates, bpy.ops.constraint.delete() in the Info window, but adding that to the script gives me a "context is incorrect" error.

Here's a way to achieve this without the Try-Except block:

import bpy

for bone in bpy.context.selected_pose_bones:
# Create a list of all the copy location constraints on this bone
copyLocConstraints = [ c for c in bone.constraints if c.type == 'COPY_LOCATION' ]

# Iterate over all the bone's copy location constraints and delete them all
for c in copyLocConstraints:
bone.constraints.remove( c ) # Remove constraint


On a more general note, the remove method is very common in blender collections, so when you try to delete a member of a collection always have a go at this in the console:

bpy.data.materials.remove( myMaterial )
bpy.context.object.modifiers.remove( someModifier )


Also note that this method usually takes the actual element reference (modifier object, constraint object, etc) as a parameter, not an index.