GAM - delete messages with multiple query criteria

6,679 views
Skip to first unread message

David Silva

unread,
Aug 10, 2015, 12:30:10 PM8/10/15
to Google Apps Manager
I am currently trying to find emails with multiple search criteria to purge from all users' inboxes.
Running a delete like so works like a charm:

gam user first...@emailaddress.com delete messages query subject:"test subject" doit

Searching by any one single criteria works very well for us but how do I use "AND" as an operator?

My issue is that I need to delete multiple messages (all have different ID's) all sent from one sender and with on subject.

Any guidance on the subject would be very helpful!

Thanks!


David Silva

unread,
Aug 12, 2015, 12:18:09 PM8/12/15
to Google Apps Manager
Resolved outside of GAM:
Well with no replies I went a different route and used a Python script that allows many different search criteria and then purges the email. You can delete as many emails that meet the criteria in the users' email and determine if you want it sent to "trash" or purged entirely.
Note: This script would also be useful for applying a label to messages that meet a certain search criteria.
If anyone is interested in the script, let me know and I can send it over.

Rishan

unread,
Dec 8, 2015, 2:08:25 PM12/8/15
to Google Apps Manager
Hi David,

Do you mind posting your python scripts here.

Also - Does anyone know whether GAM has this capability somehow?

Thanks - Rishan

Stevie Howard

unread,
Mar 16, 2016, 6:08:45 PM3/16/16
to Google Apps Manager
Hi David,

I'd be very interested in seeing that Python script too, thanks!

Jay Lee

unread,
Mar 16, 2016, 6:14:19 PM3/16/16
to Google Apps Manager

You can specify multiple search terms in one query. You just need to use single and double quotes properly:

gam all users delete messages query 'from:bad...@example.com subject:"bad message"'

See the advanced Gmail search guide for more examples:

https://support.google.com/mail/answer/7190?hl=en

Jay


--
You received this message because you are subscribed to the Google Groups "Google Apps Manager" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/118564c0-9e5b-4aef-9521-ca44d41090d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Jay Lee

Steve Mitchell

unread,
May 5, 2017, 12:23:40 PM5/5/17
to GAM for G Suite
Not having any luck with an exact replication of your quote use..... but also not finding anybody else/threads saying this works either so I'm skeptical it's effective. Tried gam all users delete messages query 'from:____@___.com subject:"Hello"' and watched it spit bacl ERROR: subject:Hello is not a valid argument for "gam <users> delete messages


On Wednesday, March 16, 2016 at 5:14:19 PM UTC-5, Jay Lee wrote:

You can specify multiple search terms in one query. You just need to use single and double quotes properly:

gam all users delete messages query 'from:...@example.com subject:"bad message"'

See the advanced Gmail search guide for more examples:

https://support.google.com/mail/answer/7190?hl=en

Jay


On Wed, Mar 16, 2016, 6:08 PM Stevie Howard <stevie...@edmonton.ca> wrote:
Hi David,

I'd be very interested in seeing that Python script too, thanks!



On Wednesday, August 12, 2015 at 10:18:09 AM UTC-6, David Silva wrote:
Resolved outside of GAM:
Well with no replies I went a different route and used a Python script that allows many different search criteria and then purges the email. You can delete as many emails that meet the criteria in the users' email and determine if you want it sent to "trash" or purged entirely.
Note: This script would also be useful for applying a label to messages that meet a certain search criteria.
If anyone is interested in the script, let me know and I can send it over.


On Monday, August 10, 2015 at 11:30:10 AM UTC-5, David Silva wrote:
I am currently trying to find emails with multiple search criteria to purge from all users' inboxes.
Running a delete like so works like a charm:

gam user first...@emailaddress.com delete messages query subject:"test subject" doit

Searching by any one single criteria works very well for us but how do I use "AND" as an operator?

My issue is that I need to delete multiple messages (all have different ID's) all sent from one sender and with on subject.

Any guidance on the subject would be very helpful!

Thanks!


--
You received this message because you are subscribed to the Google Groups "Google Apps Manager" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.
--

Jay Lee

Ross Scroggs

unread,
May 5, 2017, 12:38:56 PM5/5/17
to google-ap...@googlegroups.com
Steve,

You have to be careful with command line single quotes, especially with Windows which doesn't recognize a single quote on the command line.
Fails, single quotes with embedded double quotes: gam all users delete messages query 'from:____@___.com subject:"Hello"'
Should work, double quotes with embedded single quotes: gam all users delete messages query "from:____@___.com subject:'Hello'"

Ross

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsubscribe...@googlegroups.com.
--

Jay Lee

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.
To post to this group, send email to google-apps-manager@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Steve Mitchell

unread,
May 5, 2017, 1:34:10 PM5/5/17
to GAM for G Suite
You sir are a truly outstanding human being (and astonishingly quick to respond), it worked beautifully! I hope the clarification can help others (this thread is a prime result from Google for the topic) and look forward to using combined queries for future housecleaning.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.
--

Jay Lee

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.



--

Scott Marquardt

unread,
Oct 19, 2017, 3:44:53 PM10/19/17
to GAM for G Suite
I'm curious about OR queries as well. 

I think this is working for me:

gam print users query "orgUnitPath='/students'" | gam csv - gam user ~primaryEmail delete drivefile "query:mimeType contains 'audio' OR mimeType contains 'video'" purge

Ross Scroggs

unread,
Oct 19, 2017, 4:22:43 PM10/19/17
to google-ap...@googlegroups.com
Scott,


It looks like OR should be or

Ross

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.
To post to this group, send email to google-apps-manager@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

For more options, visit https://groups.google.com/d/optout.



--

Ian Bevan

unread,
Jun 4, 2019, 6:36:25 AM6/4/19
to GAM for G Suite
Firstly, apologies for resurrecting an old thread.
I have a few users who had their Emails migrated a few years ago and as a result some have over 10,000 draft email. Not sure what happened as I wasn't part of the migration and I don't really need to worry about that side of it. 
I can run the following    gam user XXX...@XXX.com delet messages query label:Draft which finds the offending Drafts but I'd like to add query older_than:640d.
How can I combine both queries?


On Thursday, 19 October 2017 21:22:43 UTC+1, Ross Scroggs wrote:
Scott,


It looks like OR should be or

Ross
On Thu, Oct 19, 2017 at 12:44 PM, Scott Marquardt <scott.m...@menta.com> wrote:
I'm curious about OR queries as well. 

I think this is working for me:

gam print users query "orgUnitPath='/students'" | gam csv - gam user ~primaryEmail delete drivefile "query:mimeType contains 'audio' OR mimeType contains 'video'" purge

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.



--
Ross Scroggs

+KimNilsson

unread,
Jun 4, 2019, 9:47:19 AM6/4/19
to GAM for G Suite
Checking earlier posts in the thread, it seems correct quoting should do the trick.

gam user XXX...@XXX.com delete messages query 'label:Draft older_than:640d'

Including both criteria within the quotes should set them as AND. Test and verify.

Ian Bevan

unread,
Jun 5, 2019, 10:23:15 AM6/5/19
to GAM for G Suite
Running gam user xxx...@xxx.com delete messages query 'label:Draft older_than:100d'

Gives me an error of: ERROR: older_than:100d' is not a valid argument for "gam <users> delete messages

I have since realised that it's not accurate anyway. It's picking up >10k drafts for one of my users who has none now.

If the draft is sent to trash, does the label Draft stick with it?


Kim Nilsson

unread,
Jun 5, 2019, 10:40:20 AM6/5/19
to Google Apps Manager
Try adding that as a criteria. 

NOT in:trash

Ross Scroggs

unread,
Jun 5, 2019, 10:40:21 AM6/5/19
to google-ap...@googlegroups.com
Ian,

As you are almost certainly on Windows, change the single quotes to double quotes as Windows doesn't process single quotes in any meaningful way.
gam user xxx...@xxx.com delete messages query "label:Draft older_than:100d"

Yes, a draft in the trash still has the label DRAFT so modify your query:
gam user xxx...@xxx.com delete messages query "label:draft -label:trash older_than:100d"

Ross


--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.

To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

For more options, visit https://groups.google.com/d/optout.


--

Steve - DynTech

unread,
Jun 6, 2019, 11:39:47 PM6/6/19
to GAM for G Suite
ohh, windows

Blue_Screen_of_Death.png



On Wednesday, 5 June 2019 09:40:21 UTC-5, Ross Scroggs wrote:
Ian,

As you are almost certainly on Windows, change the single quotes to double quotes as Windows doesn't process single quotes in any meaningful way.
gam user xx...@xxx.com delete messages query "label:Draft older_than:100d"

Yes, a draft in the trash still has the label DRAFT so modify your query:
gam user xx...@xxx.com delete messages query "label:draft -label:trash older_than:100d"

Ross


On Wed, Jun 5, 2019 at 7:23 AM Ian Bevan <ian....@cset.co.uk> wrote:
Running gam user xx...@xxx.com delete messages query 'label:Draft older_than:100d'

Gives me an error of: ERROR: older_than:100d' is not a valid argument for "gam <users> delete messages

I have since realised that it's not accurate anyway. It's picking up >10k drafts for one of my users who has none now.

If the draft is sent to trash, does the label Draft stick with it?


--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-manager+unsub...@googlegroups.com.


--
Ross Scroggs
Reply all
Reply to author
Forward
0 new messages