Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Artisan command for clearing all session data in Laravel

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.

like image 493
Yevgeniy Afanasyev Avatar asked Jun 07 '18 02:06

Yevgeniy Afanasyev


People also ask

How do you delete a session table in Laravel?

\Session::flush();

How do I delete a user session in Laravel?

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.

What does php artisan config clear do?

php artisan cache:clear It will remove all the cache associated with the connection to the database.


3 Answers

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/*
like image 152
mitra razmara Avatar answered Oct 19 '22 11:10

mitra razmara



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
like image 42
Devin Gray Avatar answered Oct 19 '22 11:10

Devin Gray


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:

  1. Create command
php artisan make:command FlushSessions 
  1. Create class in 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();
    }
}
  1. Run command
php artisan session:flush

Implementations for other drivers are welcome!

like image 11
jotaelesalinas Avatar answered Oct 19 '22 11:10

jotaelesalinas