Category Archives: Cron Tasks

Using Cron to Delete Old Mail

Recently, I had a client that preferred not to use the POP server on her site. She had an alternate email address and wanted all her site email forwarded to her alternate email address. This caused a problem.

The clients host server offered no method (via a control panel) to request old email be deleted as a result of age, or be deleted automatically. As mail was forwarded, copies remained on the clients server and built up and up. What was needed was to have a method for automatically delete old email, even if the client did not visit her via POP or IMAP.

The solution was relatively simple, create a cron job to delete the aged mail. The reason this was simple, the server stored each email in a separate file in a specific sub-directory off the server root. After talking with the client, it was agreed that we would automatically delete mail over 90 days old, just in case the client decided it was worth her effort to visit the site for some recent mail on some unusual occasion.

Once the aging was determined the only problem was to find the complete path to the mail directory to be kept clean. Then, assuming is the path to the mail directory to be cleaned, it was determined that a cron task could list mail that was 90 old once a week using the following cron command:


   0  0  *  *  0   `find  -atime +90`

Then, this could be used to remove old mail once a week by using the following cron command:


   0  0  *  *  0   rm `find  -atime +90`

Voila, a cron task that will keep the email for 90 days. Use the concept where you like take care of files that may age.