# Simplest way to retrieve messages (including trash)

**URL:** <https://forums.nylas.com/t/simplest-way-to-retrieve-messages-including-trash/568>\
**Category:** Question and Answers\
**Tags:** email-api\
**Created:** [November 10, 2024, 3:31pm UTC](https://forums.nylas.com/t/simplest-way-to-retrieve-messages-including-trash/568 "2024-11-10T15:31:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![neteffect](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@neteffect](https://forums.nylas.com/u/neteffect)\
**Post date:** [November 10, 2024, 3:31pm UTC](https://forums.nylas.com/t/simplest-way-to-retrieve-messages-including-trash/568/1 "2024-11-10T15:31:18Z")

</div>

I need to use a polling strategy (as opposed to webhooks) in order to sync emails into my app. I need to ensure that all emails to/from a contact are captured, including emails that have already been sent to TRASH (i.e. for Gmail). It appears that GET messages does not natively return any messages in TRASH. GET threads does include message id’s that are in trash. However, if I have to GET threads and then turn around and use a GET message for each and every message in each thread, the number of api calls is too much.

Is there any way to simply get all messages sent/received after a certain date (i.e. the last time a sync occurred) for a list of email addresses…including messages in TRASH?

---

<div class="post-metadata">

**Author:** ![omkar](https://avatars.discourse-cdn.com/v4/letter/o/bb73d2/32.png) [@omkar](https://forums.nylas.com/u/omkar)\
**Post date:** [November 13, 2024, 4:29pm UTC](https://forums.nylas.com/t/simplest-way-to-retrieve-messages-including-trash/568/2 "2024-11-13T16:29:54Z")

</div>

Hi @neteffect!

To sync all emails to/from including TRASH for Gmail, you don’t need to GET /threads first. Instead:

- Fetch all non-Trash to emails (e.g. GET [https://api.us.nylas.com/v3/messages?to=X](https://api.us.nylas.com/v3/messages?to=X))
- Fetch all non-Trash from emails (e.g. GET [https://api.us.nylas.com/v3/messages?from=Y](https://api.us.nylas.com/v3/messages?from=Y))
- Fetch all Trash to emails (e.g. GET [https://api.us.nylas.com/v3/messages?to=X&in=TRASH](https://api.us.nylas.com/v3/messages?to=X&in=TRASH))
- Fetch all Trash from emails (e.g. GET [https://api.us.nylas.com/v3/messages?from=Y&in=TRASH](https://api.us.nylas.com/v3/messages?from=Y&in=TRASH))

Using these four requests you should be able to retrieve all the messages you’re interested in.

Also, you may be able to shrink this down to just two queries if you use the `search_query_native` parameter because I believe Gmail has an OR operator. See their native syntax docs here: [Refine searches in Gmail - Computer - Gmail Help](https://support.google.com/mail/answer/7190). And here is our full guide on using Nylas’ search\_query\_native: [Searching with Nylas | Nylas Docs](https://developer.nylas.com/docs/dev-guide/search/#search-messages-and-threads-using-search_query_native)

---

<div class="post-metadata">

**Author:** ![neteffect](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@neteffect](https://forums.nylas.com/u/neteffect)\
**Post date:** [November 13, 2024, 5:27pm UTC](https://forums.nylas.com/t/simplest-way-to-retrieve-messages-including-trash/568/3 "2024-11-13T17:27:28Z")

</div>

Got it…thanks. In an effort to minimize the number of api calls (and make this applicable for all providers, not just Gmail), can you just confirm that the below approach is technically accurate:

1. GET /threads (which also includes TRASH) where a) batch list of emails (contacts) included in the any\_email and b) last\_message\_after is \> the last time we ran the GET /threads for that user and c) select=id
2. For each returned thread\_id, run a GET /threads/[thread\_id] and filter through the results for any new messages since last pull

---

<div class="post-metadata">

**Author:** ![neteffect](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@neteffect](https://forums.nylas.com/u/neteffect)\
**Post date:** [November 13, 2024, 6:07pm UTC](https://forums.nylas.com/t/simplest-way-to-retrieve-messages-including-trash/568/4 "2024-11-13T18:07:31Z")

</div>

Actually…let me rephrase the question:

1. Does your original suggestion work for all providers (not just Gmail)?
2. Instead of those 4 calls, can I use the any\_email and batch 25 emails?
3. Lastly, in the GET messages/ is the received\_after parameter ONLY for received messages or is it both received and sent?

---

<div class="post-metadata">

**Author:** ![omkar](https://avatars.discourse-cdn.com/v4/letter/o/bb73d2/32.png) [@omkar](https://forums.nylas.com/u/omkar)\
**Post date:** [November 13, 2024, 6:55pm UTC](https://forums.nylas.com/t/simplest-way-to-retrieve-messages-including-trash/568/5 "2024-11-13T18:55:59Z")

</div>

I’m just answering your second set of questions below:

1. It should work for all providers.
2. any\_email batching should work for your use case, but for Google the messages in TRASH will still be excluded unless you specify otherwise.
3. It’s for both received and sent.
