Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I see there are thousands of files in my "/tmp" directory (a CentOS machine) and almost all of them are PHP session files.
I'm worried about the possible impact this might have on my system.
Are those files ever deleted either by the OS, Apache or PHP? or I have to take care of it myself?

share|improve this question
If this is an application you have developed, then have you considered using database based sessions instead? – Zoredache May 4 '10 at 6:59
@Zoredache, For that I would need to write a session handler which uses mysql. Plus, that would put extra loading on the DB which is already very loaded. I dont know if the impact on performance would be good. – GetFree May 4 '10 at 15:16
do you see old files? – Karoly Horvath Jul 19 '12 at 11:01
@GetFree well, you wouldn't need to write one. There are existing session handlers for memcached, mysql, redis, postgres, msession, and many more. – kojiro Sep 27 '13 at 18:08

They should be deleted by the PHP garbage collector. The frequency is controlled by the session.gc_maxlifetime setting in php.ini. Possibly if this is not kicking in you have other problems.

share|improve this answer
But garbage collection exits from PHP 5.3 on. What about older versions? – GetFree May 4 '10 at 0:58
If your question is specific to a particular version of PHP, then you need to state this in your question. – dunxd Oct 24 '11 at 10:21
"garbage collection" is used in different ways for different things. The session garbage collection exists since PHP 4.0 (which introduced the session module). What's new in 5.3 is the memory garbage collection for cleaning up cyclic references of PHP variables where the reference counting mechanism keeps them alive till request end. vs. – johannes Oct 24 '11 at 11:26

On default Debian and Ubuntu, the sessions are cleaned up by cron /etc/cron.d/php5

# Look for and purge old sessions every 30 minutes
09,39 *     * * *     root   [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -delete

where /usr/lib/php5/maxlifetime gives lifetime in minutes as set in session.gc_maxlifetime.

share|improve this answer

You could setup a cron script to clean them up automatically. It's generally a good idea to test for creation date older than what the life of cookies is set up to be on your system.

Limiting cookie life is done thusly (must be done before script outputs anything):

session_set_cookie_params(1209600); # max cookie age of 14 days
# send cookie headers

Then, in your cleanup script:

find /tmp -maxdepth 1 -type f -name 'php_session_file_prefix*' -ctime +15 -exec rm -f {} \;

Then, in your crontab:

# Run daily cron jobs at 03:40 every day
40 3 * * * /path/to/
share|improve this answer

Also at reboot - as /tmp is always cleared out on reboot.

share|improve this answer
Actually, whether it is or not depends on how your system is configured. – kojiro Sep 27 '13 at 18:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.