Skip to content

Instantly share code, notes, and snippets.

@simonrjones
Last active August 29, 2015 14:16
Show Gist options
  • Save simonrjones/cdc7ae01cd857604c4d4 to your computer and use it in GitHub Desktop.
Save simonrjones/cdc7ae01cd857604c4d4 to your computer and use it in GitHub Desktop.
Delete PHP sessions from the tmp folder when you have 10,000s of them which cannot be removed with a simple rm -f /tmp/sess_* command
<?php
// Clean PHP session files when you have 10,000s of them!
// Set this to the folder that contains your session files with trailing slash
// @see http://php.net/manual/en/function.session-save-path.php
$sessionTmpFolder = '/path/to/tmp/';
$x=0;
foreach (range(0,9) as $number) {
foreach (range('a','z') as $letter) {
if (system('rm -f ' . $sessionTmpFolder . 'sess_' . $letter . $number . '*') !== false) {
$x++;
}
}
}
foreach (range('0','9') as $number) {
if (system('rm -f ' . $sessionTmpFolder . '/sess_' . $number . '*') !== false) {
$x++;
}
}
foreach (range('a','z') as $letter) {
if (system('rm -f ' . $sessionTmpFolder . '/sess_' . $letter . '*') !== false) {
$x++;
}
}
echo "Deleted $x PHP session files!" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment