Cache Cleanup Script – Delete Old Files
The Cache Cleanup Script is designed to do just what it says – clean up temporary files created in some sort of cache directory. This script is meant to accompany some other kind of script that temporarily stores information in a file.
How Cache Cleanup Works
Here’s the source code in text format.
At the top of the script, you need to set two values – $dirName and $timeLimit.
$dirName should be a local path to the directory you want to scan for old files. $timeLimit should be the maximum allowable inactive time, in seconds.
The logic of the script is very straightforward. First, we scandir() to get an array of filenames in our cache directory.
Then, we loop through each fileName. If the file was last accessed before our timeLimit, we delete the file. If not, we leave the file alone.
If the script attempts to delete a file, it will output a success or failure message. Otherwise, there is no output.
How to Implement Cache Cleanup
First, set the configuration variables to your desired settings. I intentionally refrained from using any kind of input into the script for security reasons.
Next, decide how you want to run the script.
If your host allows you to run cron jobs, the logical choice would be to make this a cron job run every so often. Depending on how long you want the temporary files to remain on your server, I’d run the cron job once a day or so.
If your host doesn’t allow cron jobs, you could manually access the script each time you want to check for idle files and delete them. This isn’t a bad choice if you want to check once a week or so, but it is kind of annoying to do it every day.
You could also incorporate this into your caching script. Every time the script creates or accesses a cached file, it could call this script (as a function) and delete unused files. This may not be a desirable option for performance reasons if you have a lot of cached files, but it wouldn’t be bad for smaller cache directories.
Anyhow, I just wrote the script. You figure out how you want to implement it.
$dirName = 'cache'; // Path to the cache directory $timeLimit = 300; // Max time since a file has been accessed // before it should be deleted if (is_dir($dirName)) { $files = scandir($dirName); } foreach ($files as $file) { if ($file == '.' || $file == '..') { continue; } if (fileatime($dirName . '/' . $file) < time() - $timeLimit) { if (unlink($dirName . '/' . $file)) { echo "$file deleted successfully."; } else { echo "$file not deleted."; } echo '<br />'; } }
mark said this on April 16th, 2008 at 8:28 am
thanks but somehow it doesn’t work for me, I get an error: Warning: Invalid argument supplied for foreach() on line 11
Walkere said this on April 16th, 2008 at 12:46 pm
You’re most likely getting that error because the scandir() call is not returning an array. That would in turn probably be because the path you used for $dirName doesn’t point to an actual directory.
Do some error checking, and make sure that you’re correctly pointing to the directory you want to scan. Remember also that the path is relative to where the script is executing.
Games said this on April 6th, 2009 at 8:34 pm
This is right here, in the present, not the future.
Donny Raybon said this on April 21st, 2009 at 9:00 pm
I would like to than the owners of this site.it has lots of good information and i hope that i can contribute to it on a daily basis.thanks and keep it up
Igor said this on March 6th, 2010 at 11:38 am
Script doesn’t work.
Warning: Invalid argument supplied for foreach() in z:\home\r\www\cache\d.php on line 11