Delete parent object hierarchy in code How can I delete hierarchy of objects in code?
When trying to delete the "parent_obj", it's written in the GUI tooltip:

bpy.ops.outliner.object_operation(type='DELETE_HIERARCHY')


I tried to select the "parent_obj" using this code:

context.scene.objects.active = bpy.data.objects['parent_obj']


But got this error:

File "~/blender-2.76-rc2-linux-glibc211-x86_64/2.76/scripts/modules/bpy/ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.outliner.object_operation.poll() failed, context is incorrect




Edit

Ok, I've found how to bypass this problem. Instead of using bpy.ops.outliner.object_operation(), I first select the objects hierarchy, and then used bpy.ops.object.delete():

bpy.data.objects[obj_name].select = True
for child in bpy.data.objects[obj_name].children:
    child.select = True
bpy.ops.object.delete()
Your own solution probably works fine in your case, but the general case where the hierarchy is a bit more nested (children of children) might still deserve a solution.

perhaps something recursive:

import bpy

def delete_hierarchy(obj):
    names = set([obj.name])

    # recursion
    def get_child_names(obj):
        for child in obj.children:
            names.add(child.name)
            if child.children:
                get_child_names(child)

    get_child_names(obj)

    print(names)
    objects = bpy.data.objects
    [setattr(objects[n], 'select', True) for n in names]

    bpy.ops.object.delete()

delete_hierarchy(bpy.context.active_object)


This function assumes no objects are currently selected, and accumulates the names of the hierarchy starting with the main object ( i used active_object, but you can pass any object reference there ). Then it selects all the found names, then runs the object_delete operator.