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.

empty all emails?

Discussion in 'E-mail Discussions' started by TheStarKillers, Oct 5, 2006.

  1. TheStarKillers

    TheStarKillers Active Member

    Joined:
    Jul 15, 2004
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    1
    How could I go about mass emptying all emails on the server, for all accounts? So that at the end, every single mailbox on the server is empty.
     
  2. beehave

    beehave Well-Known Member

    Joined:
    Jun 26, 2005
    Messages:
    104
    Likes Received:
    0
    Trophy Points:
    16
    Be carefull what you wish for...

    The following script will empty EVERY file in the mail directory as well as files in all other directory located in the mail directory.

    It will process each user one by one.

    It will tell you what files are going to be emptied and give you ten seconds to abort.

    Have not tested but should work. Use at your own risk!!!

     
  3. TheStarKillers

    TheStarKillers Active Member

    Joined:
    Jul 15, 2004
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    1
    But it will leave the emails working to receive further emails? Like if someone sends an email after I run it, will it get bounced or will the files be re-created (although without the previous emails)? Thanks!
     
  4. beehave

    beehave Well-Known Member

    Joined:
    Jun 26, 2005
    Messages:
    104
    Likes Received:
    0
    Trophy Points:
    16
    Yes, everything would still work.

    Let me know if it worked for you...
     
  5. brianoz

    brianoz Well-Known Member

    Joined:
    Mar 13, 2004
    Messages:
    1,146
    Likes Received:
    6
    Trophy Points:
    38
    Location:
    Melbourne, Australia
    cPanel Access Level:
    Root Administrator
    That script will work reasonably well if you have an mbox-based cpanel server, but it will NOT work if you have a maildir-based cpanel installation, and most newer cpanel installations work with maildir.

    The difference between maildir and mbox is, on the newer maildir based servers, each piece of mail goes into a separate randomly named file in a directory with the name "new", and the email is then deleted or moved to a directory with the name "cur" after it has been read. If you're running the old mbox format still, you really should convert over as the new format is faster (although webmail functionality changes - can't see all the mailboxes from the main login any more - so beware).

    Code:
    #! /bin/bash
    
    # this is horribly dangerous so just make sure
    echo "EMail removal program\n\n"
    who="ON THIS WHOLE SERVER"
    [ $# != 0 ] && who="for $*"
    echo "ALL EMAIL $who WILL BE REMOVED IF YOU CONTINUE"
    echo -n "DO YOU REALLY WANT TO ERASE ALL EMAIL $who ('YES' to proceed) [NO]? "
    read answer
    case "$answer" in
    YES) ;;
    *)   echo "You must answer 'YES' to continue, aborting"
         exit 1
         ;;
    esac
    
    if [ $# != 0 ]
    then
         USERS="$*"
    else
         USERS=$(ls /home)
    fi
    
    cd /home
    for user in $USERS
    do
        [ ! -d $user/mail ] && continue
        echo Processing $user ...
        if [ -d $user/mail/new ]
        then
            # the maildir version
            find $user/mail -type f -print | xargs -t rm -f
        else
            # the mbox version
            find $user/mail -type f -print | while read file
            do
                 cat < /dev/null > $file
            done
        fi
    done
    
    BTW - don't use tcsh for programming, it's an evil language, hacked together from bits of shoe string and chewing gum when there was an intermediate need. :) Use bash, it's an industry standard, and has more or less a consistent syntax. In fact don't use tcsh for ANYTHING including logging in. (Sorry - I used to teach shell programming to sysadmins for HP in the dim distant past).

    Oh yeah - this is a horrible thing to do to a server - just be sure you know what you are doing!

    Oh yes - haven't tested this fully, fairly confident it will work though, obviously I haven't a server I don't care about to test it on fully!

    This version will take a username list, and only clear out those usernames of email, might make it a little more handy. (I added that at the last moment, let me know if I messed up!)
     
    #5 brianoz, Oct 7, 2006
    Last edited: Oct 7, 2006
  6. beehave

    beehave Well-Known Member

    Joined:
    Jun 26, 2005
    Messages:
    104
    Likes Received:
    0
    Trophy Points:
    16
    brianoz, I have been thinking of converting. Thanks for the push, I needed it!
     
  7. TheStarKillers

    TheStarKillers Active Member

    Joined:
    Jul 15, 2004
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    1
    I am using mbox, so I used brianoz's version. Worked like a charm, thanks!
     
  8. RandyO

    RandyO Well-Known Member

    Joined:
    Jun 17, 2003
    Messages:
    173
    Likes Received:
    0
    Trophy Points:
    16
    why would you delete all client mail? seems to be asking for a ton of trouble... If they have an account with disk space allotment, seems they have control over what is on that account and you could be liable for any data loss that they incur due to your "wiping" of their email accounts. Hope your TOS/AUP states you can delete their files...
     
  9. TheStarKillers

    TheStarKillers Active Member

    Joined:
    Jul 15, 2004
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    1
    It's my own personal server ;). Only my files on it.
     
    #9 TheStarKillers, Oct 23, 2006
    Last edited: Oct 23, 2006
Loading...

Share This Page