cancel
Showing results for 
Search instead for 
Did you mean: 

session table - session_expires?

session table - session_expires?

Hi all,

We're using magento 2 and stores the session in mysql. I can see that session data is stored in a table called "session" and it contains "session_expires"


There is more than 1.6millions row so I want to remove the old. I can't figure out what "session_expires" contains - it seems like its the last time the session was used - is that true?

I want to delete old sessions - is there a setting I need to add to session_expires?

 

Looking forward to some clarification about this.

4 REPLIES 4

Re: session table - session_expires?

Hello @CreazioN ,

 

For some reason, you need to clear all the session data, you would usually just need to clear the var/session folder. But what if the session data is stored in database? Well, then you need to “Truncate” session  table in your Magento database. However, this will cause you to lose active sessions.

 

If you need to delete just the expired sessions use following MySQL query

DELETE FROM session WHERE session_expires < UNIX_TIMESTAMP();

--
If my answer is useful, please Accept as Solution & give Kudos

Re: session table - session_expires?

Thats the funny part - tried both:


Using time() from PHP

SELECT *, DATE_FORMAT(FROM_UNIXTIME(session_expires), '%e %b %Y %H:%i:%s') FROM session WHERE session_expires >= 1550670153

Few times with above call I get one row only and my site is very active.

 

Using MySQL unix_timestamp/now

SELECT *, DATE_FORMAT(FROM_UNIXTIME(session_expires), '%e %b %Y %H:%i:%s') FROM session WHERE session_expires >= NOW()

Both of above should give me active session thats not expired but I get zero results and my page have alot of trafic all the time.

 

 Thats why I'm not sure that its correct - bit it was my first approach the one you wrote about.

Re: session table - session_expires?

Are you still here? Answered your DM without luck. You'r solutions don't seems to do the trick, se my post.

Re: session table - session_expires?

You're helpful. Thank you, but I have next question related to topic. 
The session table take a lot of size of storage, over time. Do you know about cron for cleaner the session automatically ? Has Magento a functionally for automatically cleaner session table?