How to remove orphaned user accounts?

There should be one table containing all the user names and their individual ID numbers.
And there is another table containg all the playback history, containing both a time stamp, the user ID, and the media ID which has been played.
You need to cross-reference one with the other, then remove all the lines with the user ID of users who no longer exist from the playback history.

The whole will only require basic SQL knowledge.
But still, you want to retain a backup of the primary database file if any mishap occurs during the operation.

P.S.: make the backup only while Plex server is shut down/stopped. Otherwise the backup will be damaged.