I've created on my fresh server a directory structure like this:

/home/$username/$domain/ (for example: /home/domaincom/domain.com/) - public_html - logs - conf - tmp

Everything works well, but unfortunatelly, apache (or PHP) is not purging tmp directory automaticly. :/

Is there a simple way to purge those directories? For example a solution like logrotate, where I set directory to rotate like this: / home / * / * / logs

Problem appeared because on user temp directory has 1562074 files. :/ I'm not sure who (apache, php, external script) should clean this directory and how often.

Apache or php is not obligated to clean unused temp files (mostly sess_...)?

Thanks for any answers. :)

  • What OS? If linux, what distro?
    – MadHatter
    Feb 25, 2013 at 12:30
  • My bad, I assumed serverfault = linux. :D Linux, Debian 6.0
    – user160403
    Feb 25, 2013 at 12:33
  • @user160403: Server Fault is absolutely not limited to Linux, at all. Windows, xBSD, AIX, Solaris or whatever else is equally welcome. It's the context that matters.
    – Sven
    Feb 25, 2013 at 12:45
  • I thought that because whenever I had problem with linux, a found solution on serverfault. But as I said, my bad. :)
    – user160403
    Feb 25, 2013 at 12:47

3 Answers 3


You changed the default PHP session save path.

On a Debian system, the PHP directive session.gc_probability is set to 0, preventing old sessions from being cleaned up, and a cron job is set up that clears out the old PHP sessions - but only from the default save path!

So you can fix this in one of two ways:

  1. Change the PHP session save path back to its default.
  2. Set session.gc_probability = 1 in php.ini.
  • Yes, you're right. :)
    – user160403
    Feb 26, 2013 at 8:56

Assuming linux, write a script to parse the home directory, for each directory go into the tmp directory and remove everything over a certain date.

An example might be

 for list '/bin/ls -lRt /home/ | grep tmp/' do
  /usr/bin/find $list -atime -10  -type -f -exec rm -f {} # find+ delete files older than ten days

Obviously, you'll want to have some sanity checks in there, and my syntax may not be correct, and so on. You may want to exclude some directories, pass those to the ls command at the beginning..

Once you're happy the script finds and eliminates what you want, set it up as a cron job to run at night.


A generic solution is nearly impossible. Why? Every user's temporary folder is per-process value. Furthermore, admin can't rely on $HOME/tmp to be user's temporary folder as user may change it, and fact is, we (users) do that quite often by setting $TMP to something else. On top of all that, every process can have different setting that can't be controlled by the user at all...

Assuming you care only about the users' initial values of the $TMP environment variable you could write a script which would go through the list of users, get their default shell (this is yet another complication), find their default setting for $TMP and finally empty those folders. Second alternative is to write a program that logs-in as specific user, reads users's $TMP, and empties the folder...

However, I presume that all you want is to empty temporary files in those folders some web-application uses to store uploaded files. The easiest thing to do is to maintain your own list of those directories in a simple, plain-text file, and write a small script that will go through that list, and delete files that are older than N days...

I would rather rethink the overall structure of directories, and configure lighttpd, or whatever web-server you use, php, and the rest to store temporary files at KNOWN place so you can do cleanup easily. I would configure all of them to store temporary files in, say, /tmp/<domain>/<service> so for example.com you would have: /tmp/example.com/phpbb, /tmp/example.com/redmine, /tmp/example.com/php-sessions . Of course, you have to be careful to set ownership and permissions well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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