Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Spencer's avatar

Can't delete/reset a Laravel session variable...

$temp = '';
Session::pull('timestamps');
Session::forget('timestamps');
Session::save();
unset($temp);
session()->set('timestamps', []);
session()->push('timestamps', time());

This is at the head of my routes file... I'm trying to do some debugging on a bottleneck, but I'm getting way too many timestamps saved into the session to really be useful after a while. So I thought I'd just wipe the variables at the start of each request.... but it acts as if nothing happens and continues to add dozens of timestamps never removing the old ones... I thought that pull or forget either would work on their own... Tried both separately, together, swapped order, etc. they don't seem to be working. Then I thought just setting the value to a blank array would work... still nope. I can not clear that variable for the life of me with out using flush, but I still need other variables to persist as the debugging I'm doing is in a logged in user page.

I know I must be missing something stupid but I can't figure it out.

0 likes
2 replies
Spencer's avatar
Session::forget('timestamps');
Session::pull('timestamps');
Session::save();
foreach (Session::get('timestamps') as $key => $value)
{
    Session::pull('timestamps.' . $key);
}
unset($temp);
Session::set('timestamps', []);
Session::push('timestamps', time());

I tried using a foreach loop to remove every single entry but that gives me an Invalid argument supplied for foreach() error. Which would make me think that the array is empty but its not....

Spencer's avatar
Spencer
OP
Best Answer
Level 2

Well for this purpose I went the route of using just a Vanilla global scope variable..... since it's debugging and it doesn't really matter I guess that's fine... still frustrating that Laravel wouldn't forget/set/pull a session value.

Please or to participate in this conversation.