I was looking for a solution to delete emails from our Microsoft 365 account, as the Inbox and Archive mailboxes are full.
You know it is difficult and time-consuming to delete the emails one by one or even by a date range.
I have been looking for solutions on the internet and have never been satisfied.
But today, finally, I have got a solution and here it is:
First, we need to go and visit as an admin to:
https://purview.microsoft.com/ediscovery
Then go to > Solutions > eDiscovery
Go to > Content Search & Create a New Search for a specific user, give a search name to remember, and click create, which we will need later to use in PowerShell.
I use > boni20_23
Immediately, you will be redirected to the Query menu:
In the query menu, click> Add Sources. Search for your user exchange mailbox to add to the query and click > Save and Close
Remember and select carefully > Locations to include > Mailboxex only
For my case, I am selecting Mailboxes only (It will delete mail only unless you know what you are doing)
On the right side > Condition Builder, add the date ranges and click > Run Query
I am selecting date > between > 2020-10-02..2023-10-02
On the next pop-up, again click Run Query and wait for the results
If you have already done it, you can skip and continue, and for others, please follow accordingly.
And also, you may follow this Microsoft Article: Connect to Security & Compliance PowerShell
Download: Install and maintain the Exchange Online PowerShell module
Copy and paste on PowerShell: Install-Module -Name ExchangeOnlineManagement -RequiredVersion 3.9.0
If you see: Untrusted repository, type Yes again and press Enter
Load the Exchange Online PowerShell module
Type: Import-Module ExchangeOnlineManagement and press Enter
Connect and authenticate
Connect-IPPSSession -UserPrincipalName email@yourcompany.com
To check your session: Get-PSSession
To see the list of the searched: Get-ComplianceSearch
To delete search results as per your date range:
New-ComplianceSearchAction -SearchName "boni20_23" -Purge -PurgeType HardDelete
If you see an error:
Write-ErrorMessage : |Microsoft.Exchange.Management.Tasks.ComplianceJobTaskException|Unable to execute the task.Reason: The search "boni20_23" is still running or it didn't return any results. Please wait until the search finishesor edit the query and run the search again.At C:\Users\DEF\AppData\Local\Temp\tmpEXO_mtvu23ym.xkr\tmpEXO_mtvu23ym.xkr.psm1:1191 char:13+ Write-ErrorMessage $ErrorObject+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : WriteError: (:) [New-ComplianceSearchAction], ComplianceJobTaskException+ FullyQualifiedErrorId : [RequestId=c9291ff7-6394-9742-d901-dc467239a01a,TimeStamp=Wed, 22 Oct 2025 05:19:22 GMT],Write-ErrorMessage
Please check the status (NotStarted): Get-ComplianceSearch
Go to > Cases > Content Search > Searches > boni20_23 and Click Export
Give a name to your Exports and make changes as per your requirements, and click Export
Now wait for exporting the reports, you can check by going: Case > Content Search > Exports
Once exports are complete, follow the above instructions to purge or delete the items, or follow as below:
New-ComplianceSearchAction -SearchName "Shoeb Old" -Purge -PurgeType HardDelete
Comments
Post a Comment