laravelsessionlaravel-artisan

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 the command line?

I tried using

$ php artisan tinker  
...
\Session::flush();

But it flushes the 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 the session, again.


Solution


  • 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