How to delete all email on specific account mailbox or all account , from specific sender address from cli ?

Discuss your pilot or production implementation with other Zimbra admins or our engineers.
Post Reply
ingenetic98
Posts: 26
Joined: Thu Feb 16, 2023 1:03 am

How to delete all email on specific account mailbox or all account , from specific sender address from cli ?

Post by ingenetic98 »

Hi all ,

Did anyone know How to delete all email on specific account mailbox , from specific sender address from cli ?
ex : i have one user : userA@myzimbrauser.com
and i want to delete all email from specific sender address (ex: jerry@mycustomer.com) on userA@myzimbrauser.com mailbox ?

and , is it possible to delete all email from specific sender address ( (ex: jerry@mycustomer.com) ) from all mailbox my zimbra users account ? ( because we have one system here, which sending notification by email everyday to a lot of user here ).
and i thought that email will be same as junk mails. beside not everyone will read that notification, because they already access to that system everyday .

for now , start from December , 01 -2023 to prevent that notification email sending to my zimbra user, i adding sender address from that notification email system to blacklist zimbra .

by the end of this year i have to create zimbra backup , it will be suffering (takes too long) to backup , because there are so many emails from that notification system ( very very lot of emails ).

Please advice.

Best regards,
karl.b
Zimbra Employee
Zimbra Employee
Posts: 40
Joined: Tue Aug 02, 2022 3:31 pm

Re: How to delete all email on specific account mailbox or all account , from specific sender address from cli ?

Post by karl.b »

find all messages in a mailbox from a particular sender

Code: Select all

[zimbra@zimbra-training-1c ~]$ zmmailbox -z -m user1@ztraining.net s -t message "From:user3@ztraining.net"
num: 4, more: false

     Id  Type   From                  Subject                                             Date
   ----  ----   --------------------  --------------------------------------------------  --------------
1.  274  mess   user3                 asdfasdf                                            11/29/23 00:43
2.  273  mess   user3                 asdfasdf                                            11/28/23 21:26
3.  272  mess   user3                 test                                                11/28/23 21:20
4.  271  mess   user3                 Here It Is                                          11/28/23 21:17
You delete using the msgId so ideally if you want to build a list of msgId's, something like...

Code: Select all

[zimbra@zimbra-training-1c ~]$zmmailbox -z -m user1@ztraining.net s -t message "From:user3@ztraining.net" | egrep mess|  awk '{print $2}' > /tmp/user1_deletes
Let's just delete the last one (271), and check it again - you'd do this.

Code: Select all

[zimbra@zimbra-training-1c ~]$ zmmailbox -z -m user1@ztraining.net dm 271
[zimbra@zimbra-training-1c ~]$ zmmailbox -z -m user1@ztraining.net s -t message "From:user3@ztraining.net"
num: 3, more: false

     Id  Type   From                  Subject                                             Date
   ----  ----   --------------------  --------------------------------------------------  --------------
1.  274  mess   user3                 asdfasdf                                            11/29/23 00:43
2.  273  mess   user3                 asdfasdf                                            11/28/23 21:26
3.  272  mess   user3                 test                                                11/28/23 21:20
So if you did...

Code: Select all

[zimbra@zimbra-training-1c ~]$ for i in `cat /tmp/user1_deletes`; do zmmailbox -z -m user1@ztraining.net dm $i ;done
That should be enough to get you going, always test first.

kb
ghen
Outstanding Member
Outstanding Member
Posts: 418
Joined: Thu May 12, 2016 1:56 pm
Location: Belgium

Re: How to delete all email on specific account mailbox or all account , from specific sender address from cli ?

Post by ghen »

karl.b wrote: Fri Dec 08, 2023 10:00 pm

Code: Select all

[zimbra@zimbra-training-1c ~]$ for i in `cat /tmp/user1_deletes`; do zmmailbox -z -m user1@ztraining.net dm $i ;done
Here again, firing up zmmailbox many times to delete many messages will be *very* slow (several seconds JDK invocation time for each message).
Better pipe a bunch of "dm" commands into a single zmmailbox instance:

Code: Select all

for i in `...`
do echo dm $i
done | zmmailbox -z -m ...
I think you have to invoke zmmailbox once for each mailbox though, not sure if you could re-use a zmmailbox instance for multiple accounts.
slacker1337
Posts: 47
Joined: Fri Aug 30, 2019 8:54 am

Re: How to delete all email on specific account mailbox or all account , from specific sender address from cli ?

Post by slacker1337 »

Try using this method:

Code: Select all

$ echo "sm user1@ztraining.net" > /tmp/user1_deletes_batch
$ for i in `cat /tmp/user1_deletes`; do echo "dm $i"; done >> /tmp/user1_deletes_batch
$ zmprov -f /tmp/user1_deletes_batch
ingenetic98
Posts: 26
Joined: Thu Feb 16, 2023 1:03 am

Re: How to delete all email on specific account mailbox or all account , from specific sender address from cli ?

Post by ingenetic98 »

karl.b wrote: Fri Dec 08, 2023 10:00 pm find all messages in a mailbox from a particular sender

Code: Select all

[zimbra@zimbra-training-1c ~]$ zmmailbox -z -m user1@ztraining.net s -t message "From:user3@ztraining.net"
num: 4, more: false

     Id  Type   From                  Subject                                             Date
   ----  ----   --------------------  --------------------------------------------------  --------------
1.  274  mess   user3                 asdfasdf                                            11/29/23 00:43
2.  273  mess   user3                 asdfasdf                                            11/28/23 21:26
3.  272  mess   user3                 test                                                11/28/23 21:20
4.  271  mess   user3                 Here It Is                                          11/28/23 21:17
You delete using the msgId so ideally if you want to build a list of msgId's, something like...

Code: Select all

[zimbra@zimbra-training-1c ~]$zmmailbox -z -m user1@ztraining.net s -t message "From:user3@ztraining.net" | egrep mess|  awk '{print $2}' > /tmp/user1_deletes
Let's just delete the last one (271), and check it again - you'd do this.

Code: Select all

[zimbra@zimbra-training-1c ~]$ zmmailbox -z -m user1@ztraining.net dm 271
[zimbra@zimbra-training-1c ~]$ zmmailbox -z -m user1@ztraining.net s -t message "From:user3@ztraining.net"
num: 3, more: false

     Id  Type   From                  Subject                                             Date
   ----  ----   --------------------  --------------------------------------------------  --------------
1.  274  mess   user3                 asdfasdf                                            11/29/23 00:43
2.  273  mess   user3                 asdfasdf                                            11/28/23 21:26
3.  272  mess   user3                 test                                                11/28/23 21:20
So if you did...

Code: Select all

[zimbra@zimbra-training-1c ~]$ for i in `cat /tmp/user1_deletes`; do zmmailbox -z -m user1@ztraining.net dm $i ;done
That should be enough to get you going, always test first.

kb
hi , i already try this one :
zmmailbox -z -m user1@ztraining.net s -t message "From:user3@ztraining.net"

and delete the message id with command :
zmmailbox -z -m user1@ztraining.net dm 271

with this command i have to delete mannualy one by one on each mailbox account ?
is there any fastest way to delete all email from specific sender address ? for 1 mailbox account or all mailbox account , because almost all user (at least 200 users) on my zimbra server receive this notification mail.

how to delete all mails, in all accounts or each account, received from a specific sender,
for ex : userA@myzimbra.com , has 500 emails notification mail from specific sender on his mailbox ( inbox , junk folder or specific folder )
and i want to delete it all 500 emails from user A mailbox . is that possible ?

Please advice.
slacker1337
Posts: 47
Joined: Fri Aug 30, 2019 8:54 am

Re: How to delete all email on specific account mailbox or all account , from specific sender address from cli ?

Post by slacker1337 »

Not the best one, but it should work. Unfortunately I haven't found a way without looping through all the mailboxes to find correct message Id(s). But at the end the final batch file is executed just once.

Inspired by: https://wiki.zimbra.com/wiki/King0770-N ... on-Subject

Please use with caution!!!

Code: Select all

#!/bin/bash

[[ -z "$1" ]] && { echo "Usage: $0 user@domain.com"; exit 0; }

batch=${0}.batch

printf "\nSearching for messages with specified From address across all mailboxes..\n\n"
for user in $(/opt/zimbra/bin/zmprov -l gaa); do
    printf "$user ..."
    msgids=$(/opt/zimbra/bin/zmmailbox -z -m $user s -l 999 -t message "From:$1" | egrep -v "Id|---|^$" | awk '{ if (NR!=1) {print $2}}' | paste -sd ',')
    if [ ! -z $msgids ]; then
        echo "sm $user dm $msgids" >> $batch
        printf " Found Id(s) $msgids\n"
    else
        printf " Not found\n"
    fi
done

if [ -s $batch ]; then
    printf "\nAll messages found will be deleted.. "
    read -p "Continue? (Y/n): " confirm
    if [[ "$confirm" =~ ^[Yy]+([eE][sS])?$ ]]; then
        /opt/zimbra/bin/zmprov -f $batch
        rm -f $batch
        printf "\nDone\n\n"
    else
        printf "\nInterrupted, exiting..\n\n"
    fi
else
    printf "\nNothing has been found, exiting..\n\n"
fi
Post Reply