OK, I'm trying to delete a scene once a render finishes. I had trouble finding a way to detect when the render finishes to continue with more code. I came across "bpy.app.handlers.render_complete" which seems to work ,but causes blender to crash when trying to delete a scene. I can delete objects, images, whatever else ,but scenes tend to crash. Any solutions?


Code:
    #Remove matcap worlds 
    for matcap_worlds in bpy.data.worlds:
        matcap_worlds = matcap_worlds.name   
        if matcap_worlds.startswith("Matcap World"):
            matcap_worlds_delete = bpy.data.worlds[matcap_worlds]
            bpy.data.worlds.remove(matcap_worlds_delete, do_unlink = True)
    #Remove matcap sphere 
    for matcap_sphere in bpy.data.objects:
        matcap_sphere = matcap_sphere.name   
        if matcap_sphere.startswith("Matcap Sphere"):
            matcap_sphere_delete = bpy.data.objects[matcap_sphere]
            bpy.data.objects.remove(matcap_sphere_delete, do_unlink = True)
    #Remove matcap plane 
    for matcap_plane in bpy.data.objects:
        matcap_plane = matcap_plane.name     
        if matcap_plane.startswith("Matcap Plane"):
            matcap_plane_delete = bpy.data.objects[matcap_plane]
            bpy.data.objects.remove(matcap_plane_delete, do_unlink = True)
    #Remove matcap camera 
    for matcap_camera in bpy.data.objects:
        matcap_camera = matcap_camera.name   
        if matcap_camera.startswith("Matcap Camera"):
            matcap_camera_delete = bpy.data.objects[matcap_camera]
            bpy.data.objects.remove(matcap_camera_delete, do_unlink = True)
    #Remove matcap environments     
    for images in bpy.data.images:
        images = images.name     
        if images.endswith(".matcaps_environment"):         
            images_delete = bpy.data.images[images]
            bpy.data.images.remove(images_delete, do_unlink = True)

    #Remove matcap generator scene
    matcap_generator_delete = bpy.data.scenes['Matcap Generator']
    bpy.data.scenes.remove(matcap_generator_delete, do_unlink=True)

    bpy.context.scene.matcap_name = ''
    bpy.context.scene.matcap_materials = ''