Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Possible Duplicate:
Millions of files in php’s tmp error - how to delete?

Is there any way how to delete folder/files quicker than with command rm -rf?

It seems my disc is filled with bilions of files (sessions of php5) which were not deleted in cron so I need to delete them manually but it takes hours and it is still not helping reducing the amount. Thank you.

My command: rm -rf /var/lib/php5/*

Tried also these commands:

find /var/lib/php5 -name "sess_*" -exec rm {} \;

And

perl -e 'chdir "/var/lib/php5/" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'
share|improve this question

marked as duplicate by Michael Hampton, Sven, Ward, MadHatter, Iain Nov 15 '12 at 11:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I added to my first post commands I already tried .. Nothing seems to help ... –  Byakugan Nov 15 '12 at 3:47
1  
I would bet that the only way would be to store all those files on a different logical drive or partition, and do a quick reformat whenever you needed to clear it. –  Ryan Ries Nov 15 '12 at 4:01
    
Use find /var/lib/php5 -delete –  laebshade Nov 15 '12 at 4:50

2 Answers 2

up vote 2 down vote accepted

php5 should come with a default cron job to remove session files.

In Debian/Ubuntu, it is like the following one (direct copy from Ubuntu 12.04 LTS)

/etc/cron.d/php5

# /etc/cron.d/php5: crontab fragment for php5
#  This purges session files older than X, where X is defined in seconds
#  as the largest value of session.gc_maxlifetime from all your php.ini
#  files, or 24 minutes if not defined.  See /usr/lib/php5/maxlifetime

# 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/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete

It run every half an hour, and remove expired session base on session.gc_maxlifetime in php.ini.

So you should do following:

  1. Check if you have the above cron job file. Add it if missing.
  2. Check value of session.gc_maxlifetime in /etc/php5/apache2/php.ini

    Default value of session.gc_maxlifetime on Ubuntu is 1440sec = 24min

    session.gc_maxlifetime = 1440
    
  3. If the above 2 looks normal, try run the command line in cron job manually. That will print all error on screen.

  4. Grep for cron error in /var/log/syslog. See if they are php related.

Regarding the billions session files already exist, you have to delete them manually for now.

To put the current situation under control

service apache2 stop
mv /var/lib/php5 /var/lib/php5.delete
mkdir /var/lib/php5
chmod 733 /var/lib/php5
chmod o+t /var/lib/php5
service apache2 start

Then deleted /var/lib/php5.delete. It may takes hours. At the same time, keep an eye on file number in new /var/lib/php5 directory. If it is increasing in an abnormal way, you properly have problem other than removing files.

Run cron job command line manually

Just put the portion after root in command prompt, as follow

[ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete
share|improve this answer
    
"Regarding the billions session files already exist, you just have to delete them manually for now." But how that is my question ... My rm -rf command is running like 45 mins and so far deleted 3% o files in this folder so how to do it that is my original question here ... –  Byakugan Nov 15 '12 at 4:06
    
BTW my cronjob is like this 09,39 * * * * root [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) -deleteand it is not working ... –  Byakugan Nov 15 '12 at 4:11
    
It is not working because it is already out of control. The direct expansion is taking too long and cron job seems to have a max time limit of 1hr. I added a work around in my answer. That will also let you know if session files is increasing in an abnormal way. –  John Siu Nov 15 '12 at 4:19
    
Thank you trying that ... seems normal this folder now ... Thank you. About the cron ... how can I run it manually in command line the line you posted above? To see any errors ... –  Byakugan Nov 15 '12 at 4:24
    
@Byakugan I added at the end. Even it gives no error, you still should keep an eye on files number in that dir, maybe for a few days. Also make sure you did the chmod o+t for the new folder. –  John Siu Nov 15 '12 at 4:29

The fastest way is probably:

cd /var/lib/php5
ls -f | xargs -d "\n" rm

Also:

cd /var/lib/php5
for i in {1..999}
do
   find . -type f | head -1000 | xargs rm
done

And, if you like perl:

perl -e 'chdir "/var/lib/php5" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'
share|improve this answer
    
Already trying these seems quicker but still like 2% in one hour so I will leave it to process through night I think ... –  Byakugan Nov 15 '12 at 4:15

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