laravel's Session flush and forget methods not working as expected

18,077

Solution 1

The save() method will actaully perform the write to the file.

It seems that your application doesn't call Session::save() before it outputs content to the user on all requests.

If this method isn't called, the Session file will not get updated with the new values, and they will still be there on the next visit.

It's important to always call Session::save() if you wish to be sure stuff got removed or added and the change persists.

Solution 2

Full cache clear (either using command-line or code):

php artisan cache:clear
Artisan::call('cache:clear');

+ Full session flush:

Session::flush();

through this method you can clear all of your session data.

Solution 3

i use Session::save() method its work fine to save session data. But when i flush session using Session::flush() it does not clear all the data that is stored in Laravel session. Session::forget('value') works fine to clear session specific value.

Share:
18,077
Ewomazino Ukah
Author by

Ewomazino Ukah

I build software applications

Updated on June 07, 2022

Comments

  • Ewomazino Ukah
    Ewomazino Ukah almost 2 years

    I tried to delete a value from my session using :

    Session::forget('value')
    

    but it didn't delete!

    However, when i tried using save method like so :

     Session::forget('value')
     Session::save()
    

    it worked! (I.e. the value was deleted from the session.)

    Please - what am I doing wrong? I don't see the save method in the Laravel documentation when using Session::flush() and Session::forget().

  • Lemmings19
    Lemmings19 about 5 years
    While debugging with dd($session), Session::flush() was not permanently clearing the sessions for me until I also called the save() method. (as the accepted answer suggests)
  • shakir Baba
    shakir Baba almost 5 years
    Thanks for the answer. I was stuck with Session::forget('key'); It was removing session key temporarily, but on the next request session key still, exists. After Using Session::save(); following forget() method, it permanently removed the session key