TheStarKillers

Active Member
Jul 15, 2004
28
0
151
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.
 

beehave

Well-Known Member
Jun 26, 2005
104
0
166
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!!!

#!/bin/tcsh -f

foreach user ( `ls /home` )
echo
echo Processing $user.......
find /home/$user/mail -type f -fprint usr
echo
echo "The following files will be emptied in 10 seconds! Press Ctrl-C to stop!"
cat usr
sleep 10
foreach file ( `cat usr` )
echo "" | sed '{/^$/D}' > $file
echo
echo ..done
end
echo echo ...All done
end
 

TheStarKillers

Active Member
Jul 15, 2004
28
0
151
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!
 

beehave

Well-Known Member
Jun 26, 2005
104
0
166
TheStarKillers said:
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!
Yes, everything would still work.

Let me know if it worked for you...
 

brianoz

Well-Known Member
Mar 13, 2004
1,146
7
168
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!)
 
Last edited:

RandyO

Well-Known Member
Jun 17, 2003
173
0
166
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...