Remove linked library with Python

Is there a possibility to remove a linked library with python? Most data blocks can be removed with[insert type].remove([insert data block]), but bpy.types.BlendDataLibraries doesn’t have this method and I couldn’t find an operator to do this.

Maybe you could load all the data into the blender instance, remove what you dont want and then save a new file with the data you want, and then remove the rest of the data from your blender instance.

Just an idea.

Unfortunately it’s not about loading certain data from a library, it’s about removing an entire library from a blend file. But I’m not sure if I understand you, so please correct me if I’m wrong.

The only way I found by now is to remove all library users (groups etc. that are linked from the library) so that the library gets carbage collected after reloading the file, but this is not a good solution for me because I need that functionality as a part of an addon I’m writing and reloading the file doesn’t allow for undo-ing anymore (and if the user hasn’t saved the latest changes they get discarded too).

This is my problem (still no solution) but as that question is from 3 years ago there might be some change in the API.

So I found something (bpy.ops.outliner.lib_operation()) that would do the work, but unfortunately this method needs the library to be selected in the outliner and I can’t find a way to do this with python. Undo-ing isn’t supported as well.