Knowledgebase

How to delete automatically old database backups on your hosting account

In this article we'll show you a useful trick that you can use to automatically remove old database backups from your hosting account. This is especially useful if you have added a cron job for automatically backing up a database(s). For more information on how to do this read the tutorial on how to back up databases automatically or the corresponding shorter article version.

If you back up your database(s) automatically this will add more and more SQL dump files to your hosting account. In case you want to remove older backups you can do this either by deleting them manually or by adding a cron job that automatically deletes the older backups in the specified folder.

For example, on HostKnox accounts there's a folder labeled private_files. Let's say that in this folder you've created another one called backups, and you have added a cron job that automatically puts the backed up database dump files in that folder. The full path to that folder on HostKnox accounts will be /home/username/private_files/backups (you have to replace username with the actual username of the hosting account). So to automatically delete old database dump files you can add a cron job that executes a command such as the following:

find /home/username/private_files/backups/* -mtime +7 -exec rm {} \;

This command will remove all files in the backups folder that are older than 7 days. If you want to, you can change the number 7 in the command with a different number. Files older than the specified days will be deleted. Of course, the path to the folder is also an example one and if needed, you can change it too.

The command can actually be used to delete automatically any files in a specified folder.

You can add cron jobs from the Cron Jobs section of the Pixie control panel. After you access it click on the Add Cron Job link in the black area on the right. On the following page select from the Frequency drop-down menu how often you want the command to be executed, put the command in the Command field and click on the Add Cron Job button.

Was this answer helpful?

 Print this Article

Also Read