Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Automatic deletion of emails with cron

Discussion in 'E-mail Discussion' started by iorbita, Jun 4, 2018.

Tags:
  1. iorbita

    iorbita Registered

    Joined:
    Jun 4, 2018
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Pescara
    cPanel Access Level:
    Website Owner
    Hello,
    I have to delete emails older than 90 days with a specific email subject.

    Another solution would be to target emails that were previously moved to a specific folder, in this case the idea would be to target the folder concerned.

    The cron script that is proposed in this thread seems to suit me, it’s just missing the specific subject to target or, in the other case, the specific folder to target.

    Do you have a script to advise me? Many thanks.

    Lorenzo
     
  2. cPanelLauren

    cPanelLauren Forums Analyst II
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    3,806
    Likes Received:
    275
    Trophy Points:
    193
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    Hi @iorbita

    Looking at that command it does indicate the specific folder that you'd be targeting:

    Code:
    /home/SERVER_LOGIN_USERNAME/mail/DOMAIN_NAME/MAIL_ACCOUNT_NAME/cur
    It's removing mail from cur. You can change that to any one of the folders that are present in the mail directory for the domain for example:

    Code:
    find /home/%user/mail/example.com/mailuser/new -type f -mtime +90 -exec rm {} \;
    This would remove all mail older than 90 days in /home/$user/mail/example.com/mailuser/new

    Thanks!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. iorbita

    iorbita Registered

    Joined:
    Jun 4, 2018
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Pescara
    cPanel Access Level:
    Website Owner
    ... thank you very much for your reply! ;)
     
  4. cPanelLauren

    cPanelLauren Forums Analyst II
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    3,806
    Likes Received:
    275
    Trophy Points:
    193
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    Hi @iorbita

    You're very welcome, I'm glad I could help!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. spinge

    spinge Registered

    Joined:
    Jul 16, 2018
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Mumbai
    cPanel Access Level:
    Root Administrator
    I don't know how to do with a specific subject, but here is the solution to delete Inbox emails older than the last 90 days on a daily rotating basis, and get Cpanel to also update your mailbox size (This does not happen automatically after just deleting the emails).

    Assumption:
    Let's assume that your email is EMAILNAME@DOMAIN.COM and your cpanel account is CPANELACCOUNTNAME

    Solution:
    Go to Cpanel > Advanced > Cron Jobs > Add New Cron Job >

    a. Common Settings:
    Once Per Day (0 0 * * *)

    b. Command:
    Code:
    find /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/ -type d \( -name cur -o -name new \) -exec find {} -mtime +90 -type f -delete \; -o -name maildirsize -delete
    c. Click on 'Add new Cron Job'.

    Done!

    ---
    Similarly, to delete Trash emails older than 7 days on a daily rotating basis for the same user:

    Solution:
    Go to Cpanel > Advanced > Cron Jobs > Add New Cron Job >

    a. Common Settings:
    Once Per Day (0 0 * * *)

    b. Command:
    Code:
    find /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Trash/ -type d \( -name cur -o -name new \) -exec find {} -mtime +7 -type f -delete \;
    c. Click on 'Add new Cron Job'.

    Done!
    ---

    Note:
    mtime +90 = 90 days
    mtime +7 = 7 days

    Folder names and location for your reference:
    Inbox = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/
    Trash = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Trash/
    Spam = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.spam/
    Junk = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Junk/
    Sent = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Sent/
    Draft = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Draft/
     
    iorbita likes this.
  6. iorbita

    iorbita Registered

    Joined:
    Jun 4, 2018
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Pescara
    cPanel Access Level:
    Website Owner
    ... thank you Spinge for your participation in this discussion thread ;)
     
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice