[SOLVED] delete mails via cli based on date

Discuss your pilot or production implementation with other Zimbra admins or our engineers.
rpanis
Posts: 11
Joined: Fri Sep 12, 2014 11:54 pm

[SOLVED] delete mails via cli based on date

Postby rpanis » Tue Sep 08, 2009 7:52 am

Hi,
is it possible to delete all mail from a specific user before a specific date?

now i have to go into the users mails, use find and then delete per 100 mails
I need this because the migration went wrong and I have to redo this. If I don't delete the old mails first i got doubles.


brumdo
Posts: 9
Joined: Sat Sep 13, 2014 12:50 am

[SOLVED] delete mails via cli based on date

Postby brumdo » Tue Nov 03, 2009 10:27 am

A co-worker made a script that I then modified it to ask for variables like the account name, folder, etc.
It then uses zmmailbox to search for emails meeting the criteria that you put in the variables, uses sed to trim and create a temp file containing the messageID of the messages to delete, then uses zmmailbox again to parse the temp file, and delete the messageIDs.
I've found that even though I've set the search -l to 100000, it only deletes approx 2500 at a time. I'm not sure why. That's why it prompts for a loop in the end.
Remember to su to zimbra.
Feel free to rip me apart for my week skills. I'm sure there is plenty of room for improvement, and I'd like to hear it. :)
Here it is:
#!/bin/bash

#version .1

#
ZIMBRA_BIN=/opt/zimbra/bin

echo "Enter the username.:"

read THEACCOUNT
echo "Enter the time that you would like to delete messages up to, in mm/dd/yy format. Example 04/10/09:"

read THEDATE
echo "What folder would you like to delete these messages from?:"

read THEFOLDER
echo "You will now be deleting Messages from the $THEFOLDER folder up to $THEDATE for $THEACCOUNT."

echo "Do you want to continue? (y/N): "

read ADD
themagic ()

{

touch /tmp/deleteOldMessagesList.txt

for i in `$ZIMBRA_BIN/zmmailbox -z -m $THEACCOUNT search -l 100000 "in:/$THEFOLDER (before:$THEDATE)" | grep conv | sed -e "s/^ss*//" | sed -e "s/ss*/ /g" | cut -d" " -f2`

do

if [[ $i =~ [-]{1} ]]

then

MESSAGEID=${i#-}

echo "deleteMessage $MESSAGEID" >> /tmp/deleteOldMessagesList.txt

else

echo "deleteConversation $i" >> /tmp/deleteOldMessagesList.txt

fi

done
$ZIMBRA_BIN/zmmailbox -z -m $THEACCOUNT > /tmp/process.log

rm -f /tmp/deleteOldMessagesList.txt

echo "Completed. Run again for same user?"

read ADD

}


while expr "$ADD" : ' *[Yy].*'

do themagic

done
rpanis
Posts: 11
Joined: Fri Sep 12, 2014 11:54 pm

[SOLVED] delete mails via cli based on date

Postby rpanis » Thu Nov 05, 2009 3:37 am

thx this works!
nirinaemy
Posts: 3
Joined: Sat Sep 13, 2014 12:56 am

[SOLVED] delete mails via cli based on date

Postby nirinaemy » Mon Jan 04, 2010 5:37 am

OK! That's great! It's work very well
8134Himanshu
Outstanding Member
Outstanding Member
Posts: 217
Joined: Fri Sep 12, 2014 11:16 pm

[SOLVED] delete mails via cli based on date

Postby 8134Himanshu » Tue Mar 09, 2010 10:03 am

[quote user="brumdo"]A co-worker made a script that I then modified it to ask for variables like the account name, folder, etc.
It then uses zmmailbox to search for emails meeting the criteria that you put in the variables, uses sed to trim and create a temp file containing the messageID of the messages to delete, then uses zmmailbox again to parse the temp file, and delete the messageIDs.
I've found that even though I've set the search -l to 100000, it only deletes approx 2500 at a time. I'm not sure why. That's why it prompts for a loop in the end.
Remember to su to zimbra.
Feel free to rip me apart for my week skills. I'm sure there is plenty of room for improvement, and I'd like to hear it. :)
Here it is:
#!/bin/bash

#version .1

#
ZIMBRA_BIN=/opt/zimbra/bin

echo "Enter the username.:"

read THEACCOUNT
echo "Enter the time that you would like to delete messages up to, in mm/dd/yy format. Example 04/10/09:"

read THEDATE
echo "What folder would you like to delete these messages from?:"

read THEFOLDER
echo "You will now be deleting Messages from the $THEFOLDER folder up to $THEDATE for $THEACCOUNT."

echo "Do you want to continue? (y/N): "

read ADD
themagic ()

{

touch /tmp/deleteOldMessagesList.txt

for i in `$ZIMBRA_BIN/zmmailbox -z -m $THEACCOUNT search -l 100000 "in:/$THEFOLDER (before:$THEDATE)" | grep conv | sed -e "s/^ss*//" | sed -e "s/ss*/ /g" | cut -d" " -f2`

do

if [[ $i =~ [-]{1} ]]

then

MESSAGEID=${i#-}

echo "deleteMessage $MESSAGEID" >> /tmp/deleteOldMessagesList.txt

else

echo "deleteConversation $i" >> /tmp/deleteOldMessagesList.txt

fi

done
$ZIMBRA_BIN/zmmailbox -z -m $THEACCOUNT > /tmp/process.log

rm -f /tmp/deleteOldMessagesList.txt

echo "Completed. Run again for same user?"

read ADD

}


while expr "$ADD" : ' *[Yy].*'

do themagic

done[/QUOTE]
IT works , in My Case it gives me 1000 message a time.Lookslike some parameter that limits it give 1000 or 2500 message at a time to be searched at a time.
16117Ranjithkumar
Posts: 1
Joined: Sat Sep 13, 2014 3:25 am

[SOLVED] delete mails via cli based on date

Postby 16117Ranjithkumar » Tue Nov 19, 2013 5:45 am

Hi Guys,
I tried same script but it's not working..
If I enter what folder I want to delete after that remaining loops are not working. Automatically comes with command prompt.
Please help................
backpacker77
Posts: 38
Joined: Sat Sep 13, 2014 2:13 am

[SOLVED] delete mails via cli based on date

Postby backpacker77 » Wed Jan 08, 2014 2:49 am

Hi there,
Happy new year to all of you guys.
Thank for this great script.

Unfortunately I do have an error as well.
ERROR: zclient.CLIENT_ERROR (limit must be 1-1000)
Anyone can help me to fix this plese ? :)
Ben.
Posts: 1
Joined: Sat Sep 13, 2014 3:33 am

[SOLVED] delete mails via cli based on date

Postby Ben. » Fri Mar 14, 2014 11:29 am

[quote user="16117Ranjithkumar"]Hi Guys,
I tried same script but it's not working..
If I enter what folder I want to delete after that remaining loops are not working. Automatically comes with command prompt.
Please help................[/QUOTE]
1. You mention command prompt, this is a *nix script. Not for Windows?

2. Did you su to zimbra?

[quote user="backpacker77"]

Hi there,
Happy new year to all of you guys.
Thank for this great script.

Unfortunately I do have an error as well.
ERROR: zclient.CLIENT_ERROR (limit must be 1-1000)
Anyone can help me to fix this plese ?

[/QUOTE]
And for you I'm guessing the error is here, which means you need to change that 100k to 1k :)

for i in `$ZIMBRA_BIN/zmmailbox -z -m $THEACCOUNT search -l 100000 "in:/$THEFOLDER (before:$THEDATE)" | grep conv | sed -e "s/^ss*//" | sed -e "s/ss*/ /g" | cut -d" " -f2`
backpacker77
Posts: 38
Joined: Sat Sep 13, 2014 2:13 am

[SOLVED] delete mails via cli based on date

Postby backpacker77 » Mon Mar 17, 2014 2:21 am

And for you I'm guessing the error is here, which means you need to change that 100k to 1k :)

for i in `$ZIMBRA_BIN/zmmailbox -z -m $THEACCOUNT search -l 100000 "in:/$THEFOLDER (before:$THEDATE)" | grep conv | sed -e "s/^ss*//" | sed -e "s/ss*/ /g" | cut -d" " -f2`
[/QUOTE]
Yes indeed. Thanks

Return to “Administrators”

Who is online

Users browsing this forum: Google [Bot], MSN [Bot] and 10 guests