<p>******* Updated based on khalwat's comment ************</p>

<p>I don't think Craft does that automatically. Not sure about 3rd party plugins.</p>

<p>I wrote a custom controller function to run by Cron Job, and it's been working fine.</p>

<p>It does nothing but looping through the .sql files in the craft/storage/backups folder and delete the ones which are older than X days, based on the modified time of the file.</p>

<pre><code>public function actionDeleteOldBackups() {
    try {
        $dir = 'craft/storage/backups';
        $backupFiles = scandir($dir);

        $x = craft()-&gt;config-&gt;get('xDaysToDeleteDbBackup') ?: 30; // Keep the backup files for 30 days by default. Change it to w/e you like.

        // Craft uses Greenwitch time to name the backup files
        $xDaysAgo = strtotime('-' . $x . ' days', time());
        // depending on how you want to compare the dates, the following line can be ignored.
        $xDaysAgo = strtotime(date('y-m-d', $xDaysAgo));

        // loop through files, find the file's last modified time and compare with $xDaysAgo
        if(!empty($backupFiles)) {
            foreach ($backupFiles as $fileName) {
                if(strpos($fileName, '.sql') !== false) {
                    $filepath = $dir . '/' . $fileName;
                    if(file_exists($filepath)) {
                        $created = filemtime($filepath);
                        if($created &amp;&amp; $created &lt; $xDaysAgo) {
                            // print_r($filepath . ' '); // for testing
                            unlink($filepath); //delete the file
                        }
                    }
                }
            }
        }

        \Yii::app()-&gt;end(); // no render
    } catch (\Exception $e) {
        die('Failed to delete dated backup files.');
    }
}
</code></pre>

<p>Put the function in one of the controller of your custom plugin, kindly reminder to allow anonymous access for this function. The URL for the Cron Job will be something like:</p>

<pre>[host]/actions/pluginhandle/deleteOldBackups</pre>

CC BY-SA 3.0
2017-09-04T14:52:57.693Z
You could do it a lot easier by just looking at the `mtime` of the file. Example here: https://github.com/nystudio107/craft-scripts/blob/master/scripts/backup_db.sh#L68
andrew.welch
CC BY-SA 3.0
2017-09-04T21:43:53.403Z
Yup, that's much easier and cleaner.
Wei
CC BY-SA 3.0
2017-09-04T13:51:21.167Z
2017-09-04T22:00:07.077Z
2017-09-04T22:00:07.077Z
Wei
<p>We require a way to remove old db files from the storage|backups folder and I was wondering if there is a way to do this automatically or if someone has written a plugin that does it via a cron job?</p>

<database><back-up>
Is there a way to clear old database files from storage|backups folder automatically?
# Is there a way to clear old database files from storage|backups folder automatically?

(Asked by: username_0 on 2017-09-04)

We require a way to remove old db files from the storage|backups folder and I was wondering if there is a way to do this automatically or if someone has written a plugin that does it via a cron job?

**Tags:** database, back-up


---

## 1 answers

### Answer by username_1 on 2017-09-04. Score: 1

******* Updated based on khalwat's comment ************
I don't think Craft does that automatically. Not sure about 3rd party plugins.
I wrote a custom controller function to run by Cron Job, and it's been working fine.
It does nothing but looping through the .sql files in the craft/storage/backups folder and delete the ones which are older than X days, based on the modified time of the file.
public function actionDeleteOldBackups() {
    try {
        $dir = 'craft/storage/backups';
        $backupFiles = scandir($dir);

        $x = craft()->config->get('xDaysToDeleteDbBackup') ?: 30; // Keep the backup files for 30 days by default. Change it to w/e you like.

        // Craft uses Greenwitch time to name the backup files
        $xDaysAgo = strtotime('-' . $x . ' days', time());
        // depending on how you want to compare the dates, the following line can be ignored.
        $xDaysAgo = strtotime(date('y-m-d', $xDaysAgo));

        // loop through files, find the file's last modified time and compare with $xDaysAgo
        if(!empty($backupFiles)) {
            foreach ($backupFiles as $fileName) {
                if(strpos($fileName, '.sql') !== false) {
                    $filepath = $dir . '/' . $fileName;
                    if(file_exists($filepath)) {
                        $created = filemtime($filepath);
                        if($created && $created < $xDaysAgo) {
                            // print_r($filepath . ' '); // for testing
                            unlink($filepath); //delete the file
                        }
                    }
                }
            }
        }

        \Yii::app()->end(); // no render
    } catch (\Exception $e) {
        die('Failed to delete dated backup files.');
    }
}

Put the function in one of the controller of your custom plugin, kindly reminder to allow anonymous access for this function. The URL for the Cron Job will be something like:
[host]/actions/pluginhandle/deleteOldBackups

> **Comments:**
> **username_2**: You could do it a lot easier by just looking at the `mtime` of the file. Example here: https://github.com/nystudio107/craft-scripts/blob/master/scripts/backup_db.sh#L68  
> **username_1**: Yup, that's much easier and cleaner.  

---

