System Tools

Automatic File Deletion triggered by a specific date

A website can produce a large volume of files during operation. Those data may come from logs, application cache, temporary files or automated backups you've scheduled.

You must create some automated "clean up" profiles for those data. So not necessary files will be deleted on a regular basis, in order to never being worried with the space disk management on your web server. You'll only keep historical data needed for each purpose. 

Recommendations :
- Identify and store your files in specific locations on your web server.
- Format your file names in order to automate their processing.
- Always test your script without any deletion command to avoid nasty surprises. Otherwise, you may encounter some troubles if a bad path is specified or if there is a typing error in your command line. You may lose a large part of your file system. 

Format of the file automatic deletion command line


find /directory/subdirectory/myfiles* -mtime +15 -exec rm {} \;
 
The command "find" allows you to collect a list of all files with a name starting with myfiles in the following path /directory/subdirectory and that are 15 days old, using the option -mtime +number.
All retrieved files matching with the defined filter will be deleted through option -exec rm {} \;

Overall, -exec rm {} \; execute the rm command (deletion) on all element of the list generated by the find command.
It's very important to follow the exact syntax of that command line, especially concerning spaces.


With that file name formatting rule, you must always append the "variable" part of you file to the end of the file name : date, version or ID.

Below are some examples on how you can use Cron tasks to implement that type of automated deletion script : 

Example 1 : Deleting database backups


Those backup files are located in /home/myaccount/BACKUP/ and file names are formatted that way : BKP_MyWebSite_DB_YYYY-MM-DD, where YYYY-MM-DD represents the backup date (variable part). But with our find command, only the date attribute of the file will be used. 
Here, we have chosen to keep data only for 10 days.

find /home/myaccount/BACKUP/BKP_MyWebSite_DB_* -mtime +10 -exec rm {} \;
 

Example 2 : Deleting website filesytem backups


Those files are stored in /home/myaccount/BACKUP/ folder and formatted like that : BKP_MyWebSite_DB_DAY, where DAY represents the day of backup in letters (variable part) : Monday, Tuesday, Wednesday, etc.
In that example we decided to keep our files only for 3 days.

find /home/myaccount/BACKUP/BKP_MyWebSite_FILES_* -mtime +3 -exec rm {} \;
 

Example 3 : Purging CMS cache


In our example cache folder ils located in /home/myaccount/CMS/cache/ 
Here, all data older than one day will be deleted. All files and folders of that directory will be deleted whatever their name is. 

find /home/myaccount/CMS/cache/* -mtime +1 -exec rm {} \;

 

Example 4 : Purging some temporary files


We assume that our application automatically generate thumbnails pictures in PNG format in a dedicated folder :  /home/myaccount/TMP/.
We want to keep those files only for 5 days.

find /home/myaccount/TMP/*.png -mtime +5 -exec rm {} \;

 

To conclude ...


 
You are now able to set up Cron tasks suitable for each situation and automate cleaning processes for your website.
 
Always pay attention on the way you are organizing you files and folders : Be accurate to avoid data loss.
 
 

You have no rights to post comments

Select your language :

Newsletter

Contact