Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dpaanlka's avatar

Database Sessions Pruning

Hi frens! First post here, I'm sure I'll have many more.

Are sessions stored in the database pruned out after the session has expired?

Thanks!

0 likes
2 replies
Braunson's avatar

Yes they are cleaned out. There is another thread on Laracasts here asking the same question. Check out https://github.com/laravel/laravel/blob/master/config/session.php specifically look at the comment for Lottery.


    /*
    |--------------------------------------------------------------------------
    | Session Sweeping Lottery
    |--------------------------------------------------------------------------
    |
    | Some session drivers must manually sweep their storage location to get
    | rid of old sessions from storage. Here are the chances that it will
    | happen on a given request. By default, the odds are 2 out of 100.
    |
    */

Laravel cleans up expired session entries based on a lottery setting.

However note that not all session drivers require manual cleanup, something like Redis doesn't need it because it automatically deletes expired keys.

You can see in the StartSession middleware where it cleans up the sessions using the method collectGarbage which cleanrs the session based on the lottery config (above).

https://github.com/illuminate/session/blob/master/Middleware/StartSession.php

The default configurations are [2, 100]. It means that a random integer is chosen between 1 and 100, if it's lower or equals to 2 the cache will be cleared. (Aka you have a 2% possibility to clear the cache every call).

dpaanlka's avatar

Thank you for that reply! I searched "database session pruning" but didn't see anything that looked relevant. Glad to know it does!

Please or to participate in this conversation.