What is the artisan command for clearing all session data in Laravel, I\'m looking for something like:
$ php artisan session:clear
But apparent
If you are using the database for session driver, then empty the sessions table. Regenerating the key will cause a lot of problems if you are using single login on many subdomains. Emptying the session table helps reduce the useless data in the session table. You can delete cookies on everyone's browswer.
This thread is quite much old. But I would like to share my implementation of removing all sesssions for file based driver.
$directory = 'storage/framework/sessions';
$ignoreFiles = ['.gitignore', '.', '..'];
$files = scandir($directory);
foreach ($files as $file) {
if(!in_array($file,$ignoreFiles)) unlink($directory . '/' . $file);
}
Why I have not used linux command 'rm'?
Because PHP is one of the prerequisites for Laravel and Linux is not. Using this Linux command will make our project implementable on Linux environment only. That's why it is good to use PHP in Laravel.
The problem is that PHP's SessionHandlerInterface
does not force session drivers to provide any kind of destroyAll()
method. Thus, it has to be implemented manually for each driver.
Taking ideas from different answers, I came up with this solution:
php artisan make:command FlushSessions
app/Console/Commands/FlushSessions.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FlushSessions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'session:flush';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Flush all user sessions';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$driver = config('session.driver');
$method_name = 'clean' . ucfirst($driver);
if ( method_exists($this, $method_name) ) {
try {
$this->$method_name();
$this->info('Session data cleaned.');
} catch (\Exception $e) {
$this->error($e->getMessage());
}
} else {
$this->error("Sorry, I don't know how to clean the sessions of the driver '{$driver}'.");
}
}
protected function cleanFile () {
$directory = config('session.files');
$ignoreFiles = ['.gitignore', '.', '..'];
$files = scandir($directory);
foreach ( $files as $file ) {
if( !in_array($file,$ignoreFiles) ) {
unlink($directory . '/' . $file);
}
}
}
protected function cleanDatabase () {
$table = config('session.table');
DB::table($table)->truncate();
}
}
php artisan session:flush
Implementations for other drivers are welcome!
If you use database session , just delete all data on that table. In my case it is 'sessions' table.
An easy way to get rid of all sessions is to change the name of the session cookie. This can be easily done by changing the 'cookie' => '...'
line in config/session.php
file.
This works independently of the session storage you use and also won't touch any other data except the session data (and thus seems preferable over the renewing the app key solution to me, where you would loose any encrypted data stored in the app).
My solution Laravel
// SESSION_DRIVER=file
$files = File::allFiles(storage_path('framework/sessions/'));
foreach($files as $file){
File::delete(storage_path('framework/sessions/'.$file->getFilename()));
}
//OR
//SESSION_DRIVER=redis
Artisan::call('cache:clear'); // == php artisan cache:clear