Laravel 13.39 shipped on October 27 with a feature that solves a long-standing operational headache: a first-party session:garbage-collect command you can run on a schedule. No more relying on the probabilistic "lottery" cleanup that has been the default since Laravel 5.
The Problem with Lottery-Based Cleanup
Since the early days, Laravel has cleaned expired sessions by rolling dice on every request. The default config/session.php contains:
'lottery' => [2, 100],
This means: on 2% of requests, the framework fires the session driver's garbage collector. If your site gets steady traffic, this works well enough. But it has three predictable failure modes:
- Quiet sites never clean up. A blog with a few visits a day might go weeks without garbage collection running, leaving thousands of stale session files or database rows.
- Busy sites clean up too often. A high-traffic API might run the collector on hundreds of requests per minute, wasting CPU and I/O.
- The "unlucky" request pays the price. The request that triggers cleanup blocks while the driver scans and deletes expired sessions. On a file driver with 50,000 session files, that's a noticeable latency spike for one user.
None of these are theoretical — they show up in production logs as mysterious P99 latency bumps and bloated sessions tables.
The New Command
Laravel 13.39 adds session:garbage-collect (PRs #62104 and #62150). It does one thing: calls the current session driver's gc() method using the session.lifetime config value as the expiry threshold.
php artisan session:garbage-collect
That's it. No arguments, no options. The command is a no-op for Redis, Memcached, and other cache-based drivers because they expire keys natively via TTL. It only does work for the file and database drivers (and any custom driver that implements gc()).
Scheduling It
Register the command in your App\Console\Kernel.php:
use Illuminate\Support\Facades\Schedule;
protected function schedule(Schedule $schedule): void
{
$schedule->command('session:garbage-collect')->hourly();
}
Hourly is a sensible default for most applications. Adjust based on your session lifetime and traffic profile:
| Session lifetime | Recommended frequency |
|---|---|
| 1–2 hours | Every 15–30 minutes |
| 4–24 hours | Hourly |
| 1+ weeks | Daily |
The command finishes in milliseconds on typical datasets. Even with 100,000 expired file sessions, it usually completes under a second because it's a simple glob() + unlink() loop (or a single DELETE query for the database driver).
Disabling the Lottery
Once you schedule the command, turn off request-based cleanup to avoid double work and eliminate the latency lottery entirely:
// config/session.php
'lottery' => [0, 100],
With [0, 100], the framework never runs garbage collection during a request. The scheduled command becomes the only cleanup path — predictable, observable, and decoupled from user traffic.
What Changes in Multi-Server Deployments
If you run multiple application servers, you have two options:
Option A: Run on one server only (recommended for file driver)
$schedule->command('session:garbage-collect')
->hourly()
->onOneServer(); // requires cache-based scheduler mutex
The file driver stores sessions on the local filesystem, so garbage collection only makes sense on the server that owns the files. Use a shared network mount (EFS, NFS) if you need the file driver across multiple servers — but at that point, consider the database driver instead.
Option B: Run on every server (fine for database driver)
$schedule->command('session:garbage-collect')->hourly();
The database driver's gc() issues a single DELETE FROM sessions WHERE last_activity < ? statement. Running it concurrently from multiple servers is harmless — the query is idempotent and fast. Each server just re-executes the same cleanup.
Driver-by-Driver Behavior
| Driver | session:garbage-collect effect |
Lottery needed? |
|---|---|---|
file |
Deletes expired storage/framework/sessions/* files |
Yes, disable after scheduling |
database |
Runs DELETE on sessions table |
Yes, disable after scheduling |
redis |
No-op (Redis expires keys via TTL) | Already no-op |
memcached |
No-op (Memcached expires keys via TTL) | Already no-op |
dynamodb |
No-op (TTL-based) | Already no-op |
| Custom | Calls your SessionHandlerInterface::gc($lifetime) |
Depends on implementation |
If you use a custom driver, ensure its gc(int $lifetime): int|false method returns the number of deleted sessions (or false on failure). Laravel's built-in drivers already return int.
Observability
The command outputs a single line:
INFO Cleaned up 1,234 expired sessions.
Pipe it to your log aggregation (Laravel's default single or daily log channel, or forward to Datadog, Sentry, Loki, etc.). You now have a concrete metric: "expired sessions cleaned per run." Alert if the number stays at zero for too long (scheduler broken) or spikes unexpectedly (session fixation attempt, misconfigured lifetime).
Migration Checklist
- Upgrade to Laravel 13.39 (
composer require laravel/framework:^13.39). - Add the scheduled command to
App\Console\Kernel.php. - Set
'lottery' => [0, 100]inconfig/session.php. - Deploy and verify the command runs (check scheduler logs or run manually once).
- Monitor the first few runs — confirm expired session counts look reasonable for your traffic.
Why This Matters
Session garbage collection is one of those infrastructure details that silently drifts until it causes an incident: a sessions table that grows to 10 GB, a filesystem that hits inode limits, or a P99 latency spike that correlates with nothing obvious in your code.
The new command doesn't add magic. It gives you a standard, documented lever to move cleanup out of the request cycle and into the scheduler where it belongs — alongside your queue workers, backup jobs, and other maintenance tasks. It's a small API with a disproportionate impact on operational hygiene.
If you've ever wondered why your storage/framework/sessions directory has 40,000 files from 2023, this is the fix.