The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to auto delete all emails older than 2 years (cpanel)? Any scripts?

Discussion in 'E-mail Discussions' started by ramystyle, Jun 14, 2009.

  1. ramystyle

    ramystyle Well-Known Member

    Joined:
    Feb 9, 2004
    Messages:
    64
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Montreal
    Hello,

    I remember there used to be an Email Aging option in cPanel that is no longer available. I'm looking for something similar that I can apply to all accounts.

    Basically, I would like to be able to delete all emails on the server that are older than 2 years.

    Any scripts or way of doing this ?

    Thank you.
     
  2. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0
    You can simply run a find against all the standard mail folders
    for mail that is older than two years:
    Code:
     find -P /home/*/mail/*/*/cur -mtime '+729'
     find -P /home/*/mail/*/*/new -mtime '+729'
    
    The above will give you a list of all the mail files older than two years
    and you can either pipe that into 'xargs' for a very quick and dirty way
    of doing that pipe into a file and then have a script parse the file to
    do whatever you would like. Here is a very basic example:

    Code:
    #!/bin/bash
    IFS="$"
    
    cd /home
    
    find -P /home/*/mail/*/*/* -mindepth 1 -maxdepth 1 -mtime '+729' | while read OLDMAIL; do
      echo "Now deleting ${OLDMAIL} ..."
      rm -f "${OLDMAIL}"
    done
    
    (Incidentally the 729 is the number of days old the file must be greater)

    You could delete the "echo" line and add this basic code to a cronjob
    and have your server auto delete all mail older than 2 years or
    whatever length of time that you want.
     
    #2 Spiral, Jun 14, 2009
    Last edited: Jun 14, 2009
Loading...

Share This Page