Recent Posts






A step-by-step admin guide to removing unwanted messages from every mailbox in your tenant
Sooner or later, every Microsoft 365 administrator faces the same request: a message has landed in dozens of inboxes and it needs to disappear, fast. It could be a phishing email, a newsletter sent to the wrong audience, or an internal mail that contained confidential details. Asking each employee to delete it by hand is slow and unreliable.
The better approach is to search the whole tenant for the message and purge it centrally. This guide shows how to delete sent and received emails from Microsoft 365 mailboxes by combining a Compliance Search with a purge action in PowerShell. The same method works for items that users sent as well as items they received, because the search looks inside the mailboxes themselves.
Make sure the following are in place so the commands run without permission errors:
Install-Module ExchangeOnlineManagement
in an elevated PowerShell window.
Install-Module ExchangeOnlineManagement
Open PowerShell as an administrator and start a session with the Security & Compliance service. The search-only switch is required so that the purge action becomes available later in the session.
Connect-IPPSSession -EnableSearchOnlySession
A sign-in window will appear. Authenticate with your admin credentials, and the prompt returns once the connection is established.
Figure 1: Connecting PowerShell to the Security & Compliance service
Next, define what you are looking for. The command below creates a search called "documentation test delete" that scans every mailbox in the organization for messages whose subject contains the text "Test mail for delete". Replace both values with your own.
$Search = New-ComplianceSearch -Name "documentation test delete" -ExchangeLocation All -ContentMatchQuery '(Subject:"Test mail for delete")'
Here is what each part does:
-Name
gives the search a label you will reuse in the final step.
-ExchangeLocation All
tells the search to cover every mailbox. You can list specific
mailboxes instead to narrow the scope.
-ContentMatchQuery
holds the keyword query. Here it matches on the subject, but you
can also combine conditions such as sender or date range.
Figure 2: Creating the compliance search
Creating the search does not run it. Launch it with the command below, which uses the variable saved in the previous step.
Start-ComplianceSearch -Identity $Search.Identity
Figure 3: Starting the compliance search
Searches across a large tenant can take a few minutes. Run the following command to see how far along it is:
Get-ComplianceSearch
Look for your search name in the list. While it is running, the status reads Starting or InProgress. Wait until it changes to Completed before moving on. Running the command again every minute or so will refresh the result.
Figure 4: Status column showing the search as Completed (other entries hidden)
Get-ComplianceSearch -Identity "documentation test delete" | Format-List Name,Items,Status
If the count is higher than you expected, refine the query instead
of continuing.
Once the search is complete and you are satisfied with the results, run the purge action. It references the search by name:
New-ComplianceSearchAction -SearchName "documentation test delete" -Purge -PurgeType SoftDelete
PowerShell asks you to confirm because the action makes the matching
items inaccessible to users. Type Y and press Enter
to proceed. The action begins in the Starting state and finishes after
a short while; you can follow its progress with
Get-ComplianceSearchAction.
Figure 5: Confirming the purge action
Get-ComplianceSearchAction
The -PurgeType parameter controls what happens to
the messages:
| Purge Type | What It Does | When to Use |
|---|---|---|
| SoftDelete | Moves items to the Recoverable Items folder. Users can no longer see them, but they remain recoverable until the deleted-item retention period expires (14 days by default, configurable up to 30 days). | Safer starting point when you want a recovery window. |
| HardDelete | Marks items for permanent removal from the mailbox. | Use only when you are certain, such as for confirmed malicious content. |
For most situations SoftDelete is the safer starting point because it leaves a recovery window if the search turned out to be too broad.
Disconnect-ExchangeOnline
to close the session cleanly.
Yes. The compliance search inspects the contents of mailboxes, so a message in a Sent Items folder is found exactly like one in an Inbox, as long as it matches your query.
You can create and run the search in the Microsoft Purview portal, but the purge step is performed through PowerShell. That is why this guide combines both tools for a complete workflow.
No notification is sent. The message simply stops appearing in the mailbox.
If you used SoftDelete and the retention window is still open, the item can be restored from the Recoverable Items folder through the user's mailbox or by an administrator.
Removing a message from every mailbox no longer has to be a manual chore. With five commands you can connect to the service, define a search, run it, check its status and purge the results across your Microsoft 365 tenant. Take your time with the search query, confirm the item count, and choose the purge type carefully, and you will have a fast and dependable way to handle incidents such as phishing campaigns or accidental mass mailings.
If this tutorial helped, share it with a colleague who manages Exchange Online, and leave a comment below if you would like a follow-up on automating the process with scripts.