Can I empty elgg_users_sessions table? Too big!


My elgg users_sessions table become too big. I repair everytime. Simply may I empty truncate it on PHPmyadmin? Harmful?

Thank you

  • PHP should clean up outdated entries automatically. Make sure that these session options have been configured properly on your server:

    • session.gc_probability
    • session.gc_divisor
    • session.gc_maxlifetime


  • Some old discussion postings seem to indicate that you can empty (not delete) this table - but I can not give ANY advice here by own experience! Maybe it's best to backup the database before trying this out. Emptying the table would also result in all users getting logged out.

    Possibly it's a php.ini setting resulting in the table getting so big. It might help to set the php variable session.gc_probability to 1 for some php garbage collection. But I can't give some definite advice here either as the garbage cleanup done by php might be working differently on different servers.

  • Thank you Juho, Thank you iionly. I try your advice.

  • This issue (sessions not being cleaned up) still arises some times, as php default setting is not always set so that the garbage collector runs.

    The best way to handle this is to set the proper php settings in php.ini file :

    session.gc_maxlifetime = 604800
    session.gc_probability = 1
    session.gc_divisor = 1000

    If this option is not available, truncate is harmful and can be done on the full table.

    However, to avoid disconnecting logged in users, thruncate only sessions where "ts" is older than session duration should be removed, e.g. for a session duration of 1 day <=> 86400 seconds, we would only delete rows where "ts" is below current ts minus 86400 :

    • current timestamp : 1633332276
    • session duration : 86400
    • limit ts : 1633245876

    So SQL would be :

    DELETE FROM `elgg_users_sessions` WHERE `elgg_users_sessions`.`ts` < 1633245876;

    or better :

    DELETE FROM `elgg_users_sessions` WHERE `elgg_users_sessions`.`ts` < (UNIX_TIMESTAMP(NOW()) - 86400);