PHP Snippet – Deleting Files Older Than X Days

July 10th, 2013 - Posted by Steve Marks to PHP, Web Development.

Sometimes it is necessary to perform clean-up operations and only keep the latest files within a directory. If we take application logs for example, we might only want to keep the last 7 days worth of files. This keeps our system cleaner and prevents unnecessary disk space being taken up.

To keep these old files from building up we could do it manually, but that’s too time consuming and another thing to worry about in your already hectic life. Let’s look at how we could set up an automatic PHP script to do this for us.

Below is a basic snippet that will delete files older than 7 days old:

$days = 7;
$path = './logs/';

// Open the directory
if ($handle = opendir($path))
{
    // Loop through the directory
    while (false !== ($file = readdir($handle)))
    {
        // Check the file we're doing is actually a file
        if (is_file($path.$file))
        {
            // Check if the file is older than X days old
            if (filemtime($path.$file) < ( time() - ( $days * 24 * 60 * 60 ) ) )
            {
                // Do the deletion
                unlink($path.$file);
            }
        }
    }
}

Note: filemtime() is a PHP function that returns a unix timestamp of when the file's contents were last changed.

Simple enough right? Now... what about if we only want to delete certain files? For example, we might only want to delete PDF files and leave everything else alone. Let's take the example above and modify it to our needs:

$days = 7;
$path = './logs/';
$filetypes_to_delete = array("pdf");

// Open the directory
if ($handle = opendir($path))
{
    // Loop through the directory
    while (false !== ($file = readdir($handle)))
    {
        // Check the file we're doing is actually a file
        if (is_file($path.$file))
        {
            $file_info = pathinfo($path.$file);
            if (isset($file_info['extension']) && in_array(strtolower($file_info['extension']), $filetypes_to_delete))
            {
                // Check if the file is older than X days old
                if (filemtime($path.$file) < ( time() - ( $days * 24 * 60 * 60 ) ) )
                {
                    // Do the deletion
                    unlink($path.$file);
                }
            }
        }
    }
}

Again, relatively straightforward. All we've done different is setup an array of filetypes to delete on line 3, then get the file extension and check it's within this array (lines 14 and 15).

Tags:
This entry was posted on Wednesday, July 10th, 2013 at 8:20 am by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (4)
  1. Matthias says:

    Thank you. Works perfectly.

  2. Rinku says:

    Really,
    Huge thanks to you…
    i need it, short and simple code ..

  3. Great guy!
    I have to deleting over 1000 files per day.
    Thank you!

  4. Mick says:

    Awesome! This is exactly what I was looking for! Works perfectly!

    Thank you very much!