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

gildebrand's avatar

Prevent session file from being created

I have a route that is polled a couple of times per second on average. This route doesn't utilize sessions, and looking at the response headers from the route there is no cookies set in the response.

However, I can see that the sessions folder is increasing constantly. How can I prevent my route/controller from creating session files?

EDIT: I tried to change session driver to Redis. However now there are lot's of "empty" sessions written to Redis insted.. Problem seems to be that Laravel always create a session.

0 likes
10 replies
pmall's avatar

However, I can see that the sessions folder is increasing constantly. How can I prevent my route/controller from creating session files?

Session is started on every request. You can use database/redis for storing session. But why do you care about this?

gildebrand's avatar

Well there is no reason for a session to be created if not necessary, and we've hade some issues with the sessions folder becoming huge!

pmall's avatar

Of course the session files are natively managed so it dont become huge, dont overthink all this.

ohffs's avatar

If you move that particular route outside of the web middleware, does that help?

pmall's avatar

You can also create a middleware group ('web', 'api', etc) which doesnt include session middleware.

gildebrand's avatar

I tried changing RouteServiceProvider.php to not include the web middleware by default. However the sessions are still created.

pmall's avatar

Try to remove it from the kernel. It is also possible that php itself create a file everytime.

gildebrand's avatar

Removing it from the kernel would break the rest of my application unfortunately. But now I changed to the Redis driver and theres still lot's of sessions created (in Redis now). I guess that PHP itself can't create the Redis sessions, right?

pmall's avatar

But now I changed to the Redis driver and theres still lot's of sessions created (in Redis now).

Of course because session are started on every request. It also may be created even if the session is not actually started. It should not be a problem, other people dont seem to have problem with session files.

Please or to participate in this conversation.