Question: On my website I use PHP sessions. Session information is stored in files in my ./session path. After a few months I discovered that these session files are never deleted, by now there are 145.000 of them in this directory. 

How should these be cleaned up? Do I have to do it programmatically, or is ther a setting I can use somewhere that would have this cleanup happen automatically?

**EDIT** forgot to mention: This site runs at a provider, so I don't have access to a command line. I do have ftp-access, but the session files belong to another user (the one the webserver proces runs I guess) From the first answers I got I think it's not just a setting on the server or PHP, so I guess I'll have to implement something for it in PHP, and call that periodically from a browser (maybe from a cron job running on my own machine at home)

Answer: 
To handle session properly, take a look at <http://php.net/manual/en/session.configuration.php>.

There you'll find these variables:

* session.gc\_probability
* session.gc\_divisor
* session.gc\_maxlifetime

These control the garbage collector (GC) probability of running with each page request.

You could set those with [ini\_set()](http://php.net/manual/en/function.ini-set.php) at the beginning of your script or .htaccess file so you get certainty to some extent they will get deleted sometime.
In case someone want's to do this with a cronjob, please keep in mind that this:

```
find .session/ -atime +7  -exec rm {} \;

```

is really slow, when having a lot of files.

Consider using this instead:

```
find .session/ -atime +7 | xargs -r rm

```

In Case you have spaces in you file names use this:

```
find .session/ -atime +7 -print0 | xargs -0 -r rm

```

`xargs` will fill up the commandline with files to be deleted, then run the `rm` command a lot lesser than `-exec rm {} \;`, which will call the `rm` command for each file.

Just my two cents