Search&Delete Orphaned Tokens via Database


My Pidea version is 3.6.

I have a question of how to search&delete tokens, using SQL(pi database).
I want to write the script for above purpose, but I don’t want to use janitor tool for it because I want to keep my script on other host, not on Pidea cluster hosts.

So, could you, please give a clue of how to search such tokens?
I’ve tried token, tokeninfo, pidea_audit tables, but they don’t give such result as janitor.
Perhaps, I have to use joins\unite but there is no key to join it, I’m not such experienced in SQL.

I can found user, token(serial) was previously used by this user with such select:
SELECT user FROM pidea_audit WHERE serial = ‘TOTP***’ LIMIT 1

But how to search orphaned tokens(serials) itself?
And where to delete such tokens(what table exactly)?
Please, help.

This is not possible based on the database.

Rethink: What does an orphaned token mean? You do not have this information in the database! It only exists in the userstore!

Thank you for the answer.

Is it possible to search for orphaned tokens via API?

No, this search can take longer than the TCP timeout.

Ok, thank you for replies.