If you have a lot of 'orphans':
- Create a fresh library, and then use calibre's copy to library (delete after copy) feature to move all the 'unorphaned' books and all their metadata to the fresh library.
- Use calibre's Remove library feature to remove the old library from calibre, that will remove the library from calibre's list of libraries, it doesn't delete anything. Delete the metadata.db and any .json files from the old library directory.
- Add the books from the old library to the fresh library using calibre's Add Books from directories, including sub-directories (One book per directory . . .) option.
Then stop doing whatever you've been doing that created the orphans, such as adding and renaming author and book directories with a file manager.
BR