Artisan command for clearing all session data in Laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Clearing All Session Data in Laravel Using Artisan Commands When dealing with sessions in Laravel, you may find a need to clear all session data for all users at once. The built-in artisan command for this purpose does not exist, but you can still achieve this by following some simple steps. In this post, we'll discuss the issue and provide alternative solutions using existing commands and custom scripts. 1. Understanding Session Data in Laravel: In Laravel, session data is stored in memory or a file depending on your configuration (cookies are also used for stateless sessions). Storing session data can be essential for maintaining user information between requests, such as authentication status, logged-in users' preferences, and other sensitive information. 2. The artisan command: "php artisan tinker" Using the tinker command is a great way to interactively execute PHP code within the Laravel environment. However, it's essential to understand its limitations when working with session data. When you run the command, a new session is created for the terminal session, and executing "Session::flush()" will clear only that particular user's session data. To handle all users at once, you would need to create a loop, iterating through each active session and clearing it individually. This approach can be time-consuming if you have multiple sessions in progress. 3. Using custom scripts: To efficiently clear all session data for all users, you could leverage the "php artisan" command or develop your custom script. You could create a simple batch file with the following content: ```bash #!/bin/bash echo "Clearing all Laravel sessions..." for USER in `cat /dev/urandom | tr -cd 'a-zA-Z0-9' | fold -w 24 | head -n 25 | sed '$s/./&:/'; do php artisan tinker "\$Session::flush();" & done wait echo "All sessions cleared." ``` This script uses the output of the "tr" command to generate random usernames, which will be executed in parallel using the background functionality of the "&" operator. The session data is flushed iteratively for each generated username by running "php artisan tinker" and executing the required PHP code (Session::flush()) within a new terminal session. 4. Running the script: Once you have created your custom script, make sure it has sufficient permissions to be executed. You can run the script by simply typing its name and path in your terminal or command line interface. Make sure that your Laravel project is active in the current working directory. The script will execute all generated sessions simultaneously, ensuring a quick clearing of session data for all users. 5. Alternative solution: Modifying configuration files: If you don't mind using other methods to clear sessions, you can explore alternative solutions like updating your application configuration file. You could set the "session.memory_cache_expire" value in php.ini or change the session handler in your PHP code to use a non-default storage method (e.g., files or redis) that automatically clears sessions after a defined interval. In conclusion, if you require efficient clearing of all Laravel sessions for all users, consider creating a custom script like the one mentioned above. It provides a quick and effective solution without resorting to using other methods that may not be as reliable or applicable in your specific use case. Keep in mind that this script is an example to aid understanding; it's always best to tailor it for your project's needs. For more Laravel tutorials, including Artisan commands, please visit our [Laravel Company Blog](https://laravelcompany.com/blog).