What is the artisan command for clearing all session data in Laravel, I'm looking for something like:
$ php artisan session:clear
But apparently it does not exist. How would I clear it from command line?
I tried using
$ php artisan tinker
...
\Session::flush();
But it flushes session of only one user, I want to flush all sessions for all users. How can I do it?
I tried this:
artisan cache:clear
But it does not clear session, again.
\Session::flush();
The destroy method should remove the data associated with the $sessionId from persistent storage. The gc method should destroy all session data that is older than the given $lifetime , which is a UNIX timestamp. For self-expiring systems like Memcached and Redis, this method may be left empty.
php artisan cache:clear It will remove all the cache associated with the connection to the database.
If you are using file based sessions, you can use the following linux command to clean the sessions folder out:
rm -f storage/framework/sessions/*
UPDATE: This question seems to be asked quite often and many people are still actively commenting on it.
In practice, it is a horrible idea to flush sessions using the
php artisan key:generate
It may wreak all kinds of havoc. The best way to do it is to clear whichever system you are using.
The Lazy Programmers guide to flushing all sessions:
php artisan key:generate
Will make all sessions invalid because a new application key is specified
The not so Lazy approach
php artisan make:command FlushSessions
and then insert
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
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()
{
DB::table('sessions')->truncate();
}
}
and then
php artisan session:flush
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With