Deleted flashcards shows up in flashcard queue

Flashcards from a deleted document unexpectedly show up in the flashcard queue.

version 1.6.7 macOS

I found the reason was that those rems were still kept as “orphans” in the list of top-level rems.

There seems to be wrong with deletion in general. I see new orphans appear all the time. Also, my statistics show that the number of rems in my database only keeps growing while I deleted big documents with many rems a number of times.