1

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 3

2

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 (https://serverfault.com/a/851261/591003).

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:

  1. Create a new folder sessions_new: mkdir /var/lib/php/sessions_new
  2. Set the same permissions as for old sessions directory: sudo chmod --reference=sessions sessions_new
  3. Do the same with ownership: sudo chown --reference=sessions sessions_new
  4. Kill it! sudo rm -rf /var/lib/php/sessions
  5. 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.
  6. Wait for a long time :P
  7. When process has finished just rename the session_new directory: mv /var/lib/php/sessions_new /var/lib/php/sessions
1
  • Very helpful answer, thank you. But there is an issue with my sessions files, it is not automatically removing, i've to repeat this process every week to remove session files. I'm using LEMP Stack on Ubuntu. Any help? Apr 25, 2022 at 5:52
0

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.

2
  • I would suspect you are correct, but after an hour of running, the directory size should have decreased to some extent.
    – Bruce
    Jun 2, 2019 at 23:31
  • Let it run overnight.
    – vidarlo
    Jun 2, 2019 at 23:31
0

I 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 -a

please 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 -l

It 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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