Skip to content

Instantly share code, notes, and snippets.

@jigzstar
Last active November 11, 2023 12:17
Show Gist options
  • Save jigzstar/6511534 to your computer and use it in GitHub Desktop.
Save jigzstar/6511534 to your computer and use it in GitHub Desktop.
Zimbra - Delete a message by subject from all email accounts in a file
#!/bin/bash
# rm_message.sh user@domain.com subject
# create temp_file prior to calling this zmprov -l gaa | grep domain.com > /tmp/temp_email
if [ -z "$2" ]; then
echo "usage: rm_message.sh user@domain.com <subject>"
exit 0
else
addr=$1
subject=$2
for acct in `cat /tmp/temp_email` ; do
echo "Searching $acct for Subject: $subject"
for msg in `/opt/zimbra/bin/zmmailbox -z -m "$acct" s -l 999 -t message "from:$addr subject:$subject"|awk '{ if (NR!=1) {print}}' | grep -v -e Id -e "----" -e "^$" | awk '{ print $2 }'`
do
echo "Removing "$msg" from "$acct""
/opt/zimbra/bin/zmmailbox -z -m $acct dm $msg
done
done
fi
@jigzstar
Copy link
Author

Updated a script found on the zimbra wiki for task, think it's a handy thing to have.

@developplus
Copy link

!/bin/bash

rm_message.sh user@domain.com subject

"get message subject :" zmmailbox -z -m user@domain.com s -t message -l 50 "in:inbox date:mm/dd/yyyy"

create temp_file prior to calling this zmprov -l gaa | grep domain.com > /tmp/temp_email

if [ -z "$2" ]; then
echo "usage: rm_message.sh user@domain.com "
exit 0
else
addr=$1
subject=$2
for acct in cat /tmp/temp_email ; do
echo "Searching $acct for Subject: $subject"
for msg in zmmailbox -z -m "$acct" s -l 999 -t message "subject:$subject"|awk '{ if (NR!=1) {print}}' | grep -v -e Id -e "-" -e "^$" | awk '{ print $2 }'
do
echo "Removing "$msg" from "$acct""
zmmailbox -z -m $acct dm $msg
done
done
fi

@fokker1984
Copy link

fokker1984 commented May 3, 2017

This is what working for my zimbra Release 8.5.0.GA.3042

/opt/zimbra/bin/zmmailbox -z -m "bima@yourdomail.com" s -l 999 -t message "from:$addr "|awk '{ if (NR!=1) {print}}'|grep -v -e Id -e " -----" -e "^$" | awk '{ print $2 }'

@romale
Copy link

romale commented Feb 5, 2018

Works on 8.7

#!/bin/bash
# rm_message.sh user@domain.com subject
# or
# rm_message.sh user@domain.com


if [ -z "$2" ]; then
addr=$1
for acct in `zmprov -l gaa | grep -E -v '(^admin@|^spam\..*@|^ham\..*@|^virus-quarantine.*@|^galsync.*@)'|sort` ; do
    echo "Searching $acct"
    for msg in `/opt/zimbra/bin/zmmailbox -z -m "$acct" s -l 999 -t message "from:$addr"|awk '{ if (NR!=1) {print}}' | grep -v -e Id -e "-" -e "^$" | awk '{ print $2 }'`
    do
	#echo "Removing "$msg" from "$acct""
	#/opt/zimbra/bin/zmmailbox -z -m $acct dm $msg
	echo "Moving "$msg" from "$acct" to Trash"
	/opt/zimbra/bin/zmmailbox -z -m $acct mm $msg /Trash
    done
done
else
addr=$1
subject=$2
for acct in `zmprov -l gaa | grep -E -v '(^admin@|^spam\..*@|^ham\..*@|^virus-quarantine.*@|^galsync.*@)'|sort` ; do
    echo "Searching $acct  for Subject:  $subject"
    for msg in `/opt/zimbra/bin/zmmailbox -z -m "$acct" s -l 999 -t message "from:$addr subject:$subject"|awk '{ if (NR!=1) {print}}' | grep -v -e Id -e "-" -e "^$" | awk '{ print $2 }'`
    do
      #echo "Removing "$msg" from "$acct""
      #/opt/zimbra/bin/zmmailbox -z -m $acct dm $msg
      echo "Moving "$msg" from "$acct" to Trash"
      /opt/zimbra/bin/zmmailbox -z -m $acct mm $msg /Trash
    done
done
fi

@rmcatog
Copy link

rmcatog commented May 20, 2020

this doesn't work if the Subject contains a "-" character. Must edit the script line from
grep -v -e Id -e "-" -e "^$"
to
grep -v -e Id -e "----" -e "^$"

@jigzstar
Copy link
Author

jigzstar commented May 20, 2020

Thanks, will update. This worked for me on the older zimbra 7 server

@rmcatog
Copy link

rmcatog commented Jun 19, 2020

How about removing an email only by subject without specifying the sender addr?

@bonniebm
Copy link

the script seems not to be working on zimbra version 9

@Gelogo
Copy link

Gelogo commented Jan 27, 2022

One tip, switch the grep and awk in zmprov command:

... | grep -v -e Id -e "-" -e "^$" | awk '{ print $2 }'
... | awk '{ print $2 }' | grep -v -e Id -e "--" -e "^$" -e "Type"

This will firstly filter on msgId column (numbers only) then clear the output.
Doing this the subject can contain whatever and doesn't get filtered out by grep.

@kidx13
Copy link

kidx13 commented Jan 31, 2022

how if i want to delete by range of date ?

@Gelogo
Copy link

Gelogo commented Feb 3, 2022

From Zimbra doc, you can use Zimbra command to filter messages:
zmmailbox -z -m user@domain.com s -t message -l 50 "in:inbox date:01/01/2015"

Ref: https://wiki.zimbra.com/wiki/Deleting_messages_from_account_using_the_CLI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment