I have set up a Scheduled Backup (Database and Media) which works fine. However, it's done every day and I now have 72 of them!

I only need to keep the last 7 days worth. Is there any way of deleting the older backups? And is there a way of automatically limiting the number of backups kept?

3 Answers 3


yes you can survive your files with my custom function.

public function deleteBackupFiles() {

        $filelist = array();
        if ($handle = opendir(Mage::getBaseDir('var') . DS . "backups" . DS)) {
            while ($entry = readdir($handle)) {
                //if ($this->endsWith($entry, "_db.gz")) {            
                if ($this->endsWith($entry, "nomedia.tgz")) {
                    $filelist[] = $entry;

        foreach ($filelist as $each_file) {
            /* echo "<br> $filename was last modified: " . date ("Y-m-d h:i:s", filemtime(Mage::getBaseDir('var') . DS . "backups" . DS.$each_file));

              $last_modi = filemtime(Mage::getBaseDir('var') . DS . "backups" . DS.$each_file); */
            if (time() - filemtime(Mage::getBaseDir('var') . DS . "backups" . DS . $each_file) >= 7 * 86400) { // where 7 is days
                unlink(Mage::getBaseDir("var") . DS . "backups" . DS . $each_file);


cron tab should look like

                    <cron_expr>0 4 * * *</cron_expr>

Add this function to your custom model / observer and run it with cron expression.

let me know if i could help you more

  • Looks great. Don't know what my 'custom model / observer' is, or how to run it with a cron expression. Mar 16, 2015 at 9:57
  • @PhilStewart, i have added cron tab just add this to your config.xml file so it will delete all back files which is older then 7 days at 4 am
    – liyakat
    Mar 16, 2015 at 10:17
  • Great. The cron looks straightforward - but what do I do with the function? Mar 16, 2015 at 10:51
  • put it in some model and change yourmodel in cron tab to define model
    – liyakat
    Mar 16, 2015 at 12:42
  • Sorry, I don't understand... How do I put it in 'some model'? Mar 17, 2015 at 16:04

You may also try to write a shell script, like shown here: https://superuser.com/questions/268344/how-do-i-delete-all-but-10-newest-files-in-linux (just adapt the number of days)

After that, setup a cron job outside of Magento and run it every day. Might also work.



If you have shell access.

find var/backups -type f -mtime +14 -delete

Removes all files older than 14 days from within the backup directory.

You could easily convert it into a script to run as a cronjob outside Magento.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.