logo
search
Others

How to Delete Emails from a Specific Mailbox Folder Using PowerShell

Muhammad TalhaMuhammad Talha Oct 10, 2026 869 views

Question details

The user needs to find a way to remove messages within a specific date range from one selected Exchange mailbox folder without deleting contents from other folders like the Inbox or Sent Items.

How to Delete Emails from a Specific Exchange Mailbox Folder Using PowerShell
Product
Microsoft Exchange / PowerShell
Device & OS
not provided
Scenario
Performing targeted email cleanup or compliance removal by deleting messages from a designated folder based on specific date criteria.
Observed behavior
The administrator needs a PowerShell script or Exchange compliance search method that accurately identifies and targets the exact folder path and mailbox for deletion.
Before you start

Before executing any PowerShell deletion commands, ensure you are assigned the 'Search And Purge' or 'Mailbox Import Export' roles in Exchange, and always run a search query to preview the results before initiating the deletion.

Solution 1Recommended

Use Microsoft 365 Compliance Search and Purge

The most modern and recommended approach for Exchange Online is to use the Security & Compliance PowerShell cmdlets to search for and purge the specific emails.

Microsoft recommends using the New-ComplianceSearch cmdlet instead of the deprecated Search-Mailbox cmdlet for Exchange Online. You can specify the target mailbox, the exact folder path, and the date range to safely remove the desired messages.

1
Connect to Security & Compliance PowerShell

Open PowerShell as an administrator and run 'Connect-IPPSSession' to authenticate and connect to the Microsoft 365 compliance center.

2
Create a Compliance Search

Use the New-ComplianceSearch cmdlet. Format your query to specify the mailbox and the folder path, for example: New-ComplianceSearch -Name "RemoveTargetEmails" -ExchangeLocation "user@domain.com" -ContentMatchQuery 'folderpath:"TargetFolder" AND received:01/01/2023..01/31/2023'.

3
Start the Search

Run the command 'Start-ComplianceSearch -Identity "RemoveTargetEmails"' to begin scanning the mailbox for matching items.

4
Purge the Found Emails

Once the search is complete, execute 'New-ComplianceSearchAction -SearchName "RemoveTargetEmails" -Purge -PurgeType HardDelete' to permanently remove the messages from the specific folder.

Use Microsoft 365 Compliance Search and Purge
Maximum Deletion Limit: A compliance search purge action can only delete a maximum of 10 items per mailbox at a time. For larger deletions, you may need to run the purge command multiple times or use an iterative script.
Free Microsoft Office alternative

Looking for a Lightweight Alternative for Daily Office Tasks?

While managing Exchange mailboxes requires specialized Microsoft admin tools like PowerShell, your daily document creation shouldn't be complicated. WPS Office provides a lightweight, completely free, and highly compatible alternative to Microsoft Office for creating documents, spreadsheets, and presentations without complex licensing.

100% compatible with Microsoft Word, Excel, and PowerPoint formatsLightweight application that uses minimal system resourcesFree to download and use with a highly familiar user interfaceSeamless migration for individuals and teams looking for cost-effective productivity tools
microsoft office alternative - wps office

Frequently Asked Questions

How do I find the exact folder path to use in my PowerShell query?

You can use the Get-MailboxFolderStatistics cmdlet. Run 'Get-MailboxFolderStatistics -Identity "user@domain.com" | Select Name, FolderPath' to view a list of all folders and their exact path strings to use in your ContentMatchQuery.

Can I recover emails deleted using the Purge action in PowerShell?

If you use the 'SoftDelete' purge type, the items are moved to the Recoverable Items folder and can be recovered by the user until the retention period expires. If you use 'HardDelete', the items are permanently removed and can only be restored from an external backup.

Why is my New-ComplianceSearchAction command failing?

This usually happens due to a lack of permissions. To run purge actions, your admin account must explicitly be assigned the 'Search And Purge' role in the Microsoft 365 Defender or Compliance portals. This role is not assigned to global administrators by default.

Does Search-Mailbox work for subfolders automatically?

When querying a specific folder path, standard searches may only target the parent folder. To ensure subfolders are targeted or excluded, you must explicitly define the folder path including the subfolder structure in your query syntax.