# How can I remove a linked library with Python?

I'm working on an add-on. The purpose is to link and un-link groups from another blend file. Thanks to stack exchange and the Blender documentation this works pretty well.

This means I can load a library and import a group from it.

with bpy.data.libraries.load(path, link=True) as (sourceData, targetData):
targetData.groups = sourceData.groups


Then I search the library and extract the group to create an instance of the group:

instance = bpy.data.objects.new(name, None)
instance.dupli_type = 'GROUP'
instance.dupli_group = group


The result is a dupli-group object that links in the other blend file. (the link is persistent and will survive a reload .. that is what I want).

My idea is to do the opposite. If the user does not want this link anymore, I would like to get rid of

• the instance
• the group

This works pretty nicely too for this instance:

context.scene.objects.unlink(instance)
bpy.data.objects.remove(instance)


and the group:

group.user_clear()
bpy.data.groups.remove(group)


I saw (in the outliner/Blendfile)there is still a user to the library. I guess it is the group object. So I removed that too:

for object in  library.users_id:
for object in  library.users_id:
object.user_clear()
bpy.data.objects.remove(object)


Now the outline shows me the library without any user ... but

It seems there is no API call to do that. bpy.data.libraries.remove() does not exist. There is a load but no unload.

Saving/Loading the file removes the library. But this is far away from any comfort.

Reloading the blend file destroys the current context. E.g. undo would not be possible anymore. It also involves unnecessary file operations, that leads to another cycle in the backup files. Beside of that it simply feels incorrect to use such a workaround (Imagine you need to restart your PC each time you delete a file)

I found this one: Q: Proper way to remove unused linked Group data-blocks but this does not answer my question as I want to get rid of the library not just the group.

Do I miss something?

Thank you Monster

• Great question! Would be awesome to have this as add-on. Major issue is removing the group IMO, unfortunately I also could not find a proper way of doing this. Why not starting a bounty to get more attention?
– p2or
Jan 15 '16 at 10:49
• I really think this is not implemented. I doubt a bounty will help there any further. Jan 15 '16 at 19:53

Quite an old thread, but it seems there is finally a solution available. Newer versions of Blender provide the batch_remove method in the bpy.types.BlendData type. So you can call:

bpy.data.batch_remove(ids=(my_library,))


my_library is a bpy.types.Library object in this case and no string/name.

Keep in mind that to this date, the documentation states, that this method is considered experimental, but as long as you don't deal with blend file critical data blocks like scenes, it seems you should be fine.

Using a python "with" statement you don't need to unload/remove/delete the library, because when you leave the block of with, the resources are cleanly released regardless of the outcome of the code in the block. I suggest this post to better understand the use of with.

• Unfortunately I do not see how this helps. The "with" clause is used along with load() - which is a (timely) separate step. I do not know what exactly the context managers are doing beside closing the file? My experiments didn't show any success in getting rid of the library reference. Jul 6 '16 at 6:52
• You don't need to close the library, because when you go out of the with statement you have already closed it Jul 6 '16 at 6:58
• My aim is less to close it. It automatically does this as you correctly state in your answer. I want to remove the reference to the library after the last user gets removed. Jul 6 '16 at 7:10