Wordpress Bloated "session_token" in MySQL Database

admin

Administrator
Staff member
I'm running a wordpress site on a vps. My site has too many users using the wordpress login, so every couple of days the site login form stop working and keeps redirecting the user to the login form again and again (not to the dashboard). After investigation and trying to figure out whats going on i logged into phpmyadmin and found out that the "session_tokens" field in "wp_usermeta" was getting bloated (i.e. too many connections/idle connections). I just cleared all values and everything started to work.

Now i'm looking for a way to clear that field from mysql db every 12 hour. I looked up Wordpress code reference (
<a href="https://developer.wordpress.org/reference/classes/wp_session_tokens/destroy_all_for_all_users/" rel="nofollow">https://developer.wordpress.org/reference/classes/wp_session_tokens/destroy_all_for_all_users/</a>) and found a function that destroy all session tokens for all users.

Code:
final public static function destroy_all_for_all_users() {
    $manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
    call_user_func( array( $manager, 'drop_sessions' ) );
}

If there is a way to create a simple php file that i could run every time to clear this will be great (I can then create a cron job), but I'm not sure if that's possible or how it can be done. Any help is appreciated.

Thanks.

P.s.I did try a plugin to logout idle connections, but that didn't help at all.