Short description of error
Blender undo steps do not always properly clear all no-more-used libraries, especially when dealing with indirect hierarchies.
Exact steps for others to reproduce the error
With those files:
- Open TEST.blend
- Link a collection from 2.blend (2.blend has an object linked from 1.blend; now the Outliner has 2 linked files)
- Undo the operation (Ctrl+Z)
- You can see in outliner that both libraries (and some indirectly-linked datablocks) are still there.