Can't delete php sessions
I have read dozens of posts on his subject, yet cannot find a solution that works for me.
I have a user account. PHP sessions for that account are stored in
/home/user/tmp
Apache start tossing errors that i can't open sessions because the device is full.
df and df -i both show me that the device has plenty of space.
So I look to he /home/user/tmp directory. It shows the size of the directory is 901MB.
I wan to delete the session files from the directory.
I have tried
rm -rf /home/user/tmp/sess_*
I let his run for over an hour then used cntr+C to kill the command. The directory still says 901 MB.
I have rebooted the system, the directory still shows at 901MB.
I have tried
find /home/*/tmp -type f -name 'sess_*' -ctime +5 -delete
Just like with rm -rf I let the command run for over an hour and killed it.
The directory still shows 901MB.
I am issuing these commands as root user. I am using ubuntu 16.04. This is a server so only command line options will work.
What can I do to remove these session files?
3 Answers
Often you can't delete session files even with command like sudo rm -rf /var/lib/php/sessions/* because a path name expansion occurs BEFORE the sudo ().
You can try another command like sudo sh -c "rm -rf /var/lib/php/sessions/*" but if you have tons of files you'll get an answer that rm: Argument list too long because the list of session files is usually very huge (up to millions of files).
I propose a really simple way:
- Create a new folder sessions_new:
mkdir /var/lib/php/sessions_new - Set the same permissions as for old sessions directory:
sudo chmod --reference=sessions sessions_new - Do the same with ownership:
sudo chown --reference=sessions sessions_new - Kill it!
sudo rm -rf /var/lib/php/sessions - You don't need to wait a result for a long time. You can check it immediately in a new terminal window by
sudo find /var/lib/php/sessions/. -type f|wc -l. This command counts the number of files inside your sessions folder. Run it two times. If the second time you run the command, you get a lower number than the first time, then the process is going in the right direction. - Wait for a long time :P
- When process has finished just rename the session_new directory:
mv /var/lib/php/sessions_new /var/lib/php/sessions
If it's a lot of small files, deleting can take some significant time. Let it run until it finishes. It should eventually finish.
I did a quick test on my system, and deleting 100k empty files on a SSD disk with ext4 took 1.14 seconds. If you have tens of millions of files, it may take a good while.
2I think Dzmitry Kulahin's solution works, but in my case I ran out of space so I couldn't create the new "sessions_new" directory. I solved using command line php in interactive mode:
sudo php -aplease note that you must use sudo, otherwise subsequent commands have no privileges to operate.
Write this sequence on a single line, it is necessary to write it on a single line because in interactive mode, each press of the enter key immediately executes the command:
$listfile = scandir("/var/lib/php/sessions/");
foreach ($listfile as $file) { if (!is_dir("/var/lib/php/sessions/".$file)) { unlink("/var/lib/php/sessions/".$file); }
}Press the enter key and the files start deleting.
You can monitor the cancellation by opening another terminal and typing:
sudo find /var/lib/php/sessions/. -type f|wc -lIt will show you the number of files in the directory, repeating this the command several times you will see the number go down as the files are deleted.