Endpoint Delete Messages - Using Search Condition (Bulk Delete)
Name
delete_messages_by_search
Description
Delete multiple messages for a specified search condition / mailbox / folder (Bulk delete outlook messages)
Parameters
Parameter | Required | Options | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Name:
Label: User ID or Email The Microsoft Mail user ID GUID or email address for the account to read messages from. (Examples: '4ff54a77-9c09-4274-958f-83bdc0a53900' or 'first.last@domain.com') |
YES | |||||||||||||||||||||||||||||||||||||||||||||||||||||
Name:
Label: Email Body Format |
YES |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Name:
Label: MailFolderId |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Name:
Label: Search Criteria Data filter (e.g. somecolumn -eq 'somevalue'), you can use Date time functions for dynamic date. You can use keywords like now,today,yesterday,monthstart,monthend,yearstart,yearend,weekstart,weekend along with operator + / - [hour,minute,second,day,month,year] |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
Name:
Label: Order By Order the specified column by ascending or descending order |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
Name:
Label: Page Size The number of records to include in each page of results |
Output Columns
Label | Data Type (SSIS) | Data Type (SQL) | Length | Description |
---|---|---|---|---|
MessageId |
DT_STR
|
varchar(255)
|
255 | |
Status |
DT_I4
|
int
|
||
ErrorCode |
DT_WSTR
|
nvarchar(1000)
|
1000 | |
ErrorMessage |
DT_WSTR
|
nvarchar(1000)
|
1000 |
Input Columns
Label | Data Type (SSIS) | Data Type (SQL) | Length | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
There are no Static columns defined for this endpoint. This endpoint detects columns dynamically at runtime. |
Examples
SSIS
Use Outlook Mail (Office 365) Connector in API Source or in API Destination SSIS Data Flow components to read or write data.
API Source
Required Parameters | |
---|---|
User ID or Email | Fill-in the parameter... |
Optional Parameters | |
MailFolderId |

API Destination
Required Parameters | |
---|---|
User ID or Email | Fill-in the parameter... |
Optional Parameters | |
MailFolderId |

ODBC application
Use these SQL queries in your ODBC application data source:
Delete multiple messages with search condition
This example shows how to delete multiple outlook messages for a specified User Id / Mailbox Folder and Search Condition. We used multiple conditions to search and delete those emails. For example search mail with attachments only, sent after 2023-03-01, subject contains string 'Your Microsoft invoice' and email sent from microsoft-noreply@microsoft.com. It will delete only those emails which meets all these conditions.
SELECT * FROM delete_messages_by_search
--To delete max N messages set limit below
--LIMIT 5
WITH(
MailFolderId='INBOX', --or use mailbox ID
UserId='me', --or use "user-id" or use "email"
--UserId='1487fe8b-f09f-4015-a817-b8b9fe2a3edc',
--search mail with attachments, sent after 2023-03-01, subject contains string "invoice" and from email is microsoft-noreply@microsoft.com
--Your Microsoft invoice G073237208 is ready
SearchCriteria='hasAttachments eq true and sentDateTime gt 2025-07-01 and contains(subject, ''Your Microsoft invoice'') and from/emailAddress/address eq ''microsoft-noreply@microsoft.com'' '
)
--Check this link to learn more about search conditions: https://learn.microsoft.com/en-us/graph/filter-query-parameter?tabs=http
SQL Server
Use these SQL queries in SQL Server after you create a data source in Data Gateway:
Delete multiple messages with search condition
This example shows how to delete multiple outlook messages for a specified User Id / Mailbox Folder and Search Condition. We used multiple conditions to search and delete those emails. For example search mail with attachments only, sent after 2023-03-01, subject contains string 'Your Microsoft invoice' and email sent from microsoft-noreply@microsoft.com. It will delete only those emails which meets all these conditions.
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM delete_messages_by_search
--To delete max N messages set limit below
--LIMIT 5
WITH(
MailFolderId=''INBOX'', --or use mailbox ID
UserId=''me'', --or use "user-id" or use "email"
--UserId=''1487fe8b-f09f-4015-a817-b8b9fe2a3edc'',
--search mail with attachments, sent after 2023-03-01, subject contains string "invoice" and from email is microsoft-noreply@microsoft.com
--Your Microsoft invoice G073237208 is ready
SearchCriteria=''hasAttachments eq true and sentDateTime gt 2025-07-01 and contains(subject, ''''Your Microsoft invoice'''') and from/emailAddress/address eq ''''microsoft-noreply@microsoft.com'''' ''
)
--Check this link to learn more about search conditions: https://learn.microsoft.com/en-us/graph/filter-query-parameter?tabs=http';
EXEC (@MyQuery) AT [LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY];