Use Search-Mailbox to Remove Thousands of Items from an Exchange Online Mailbox

The Need for a Nice Clean Mailbox

Note: Search-Mailbox is due for deprecation on July 1, 2020. See this post for more information.

Another question, this time from the Facebook Office 365 group:

How can i delete a whole bunch of emails in a shared mailbox using the online mail browser? (instantly)

It’s quite common to find that a clean-up is needed for shared mailboxes. It might be possible to do the job manually by selecting and removing the messages, or using OWA’s Cleanup mailbox option, but both options can take a long time to run and move items into the Deleted Items folder, where you might want to remove the items permanently.

Cleaning Options

The OWA options are user-driven and can be applied to any mailbox to which a user has access. The other available options are administrative actions. Let’s assume that you want to cleanup a mailbox with an Inbox folder of 100,000 items. This is well under the Exchange Online folder limit of 1 million items, but it’s still a bunch of data to process. Here are three obvious actions that can be taken:

  1. Use the Search-Mailbox cmdlet to remove items from the mailbox. The upside is that Search-Mailbox can remove the items permanently; the downside is that Search-Mailbox only returns 10,000 items at a time, so it can only remove 10,000 items. Ten searches are needed to remove our 100,000 items.
  2. Apply an Exchange mailbox retention policy to the Inbox folder to remove all items after they are “x” days old (let’s say 7). The upside is that the Managed Folder Assistant does its processing in the background and can remove items permanently. The downside is that the Managed Folder Assistant might not process the mailbox for another seven days (the workcycle used in Exchange Online), and you will have to wait for its completion to see the results of its work.
  3. Create a new mailbox and switch it in to replace the old mailbox. The upside is that you get a clean mailbox immediately, which is what you want. The downside is that you might need to recover items from the old mailbox before it is discarded. As was pointed out to me, make sure that the new mailbox has the LegacyExchangeDN of the old mailbox as a proxy X.500 address so that messages sent to the mailbox using addresses stored in user autoaddress caches don’t NDR.

Other available methods include creating some Exchange Web Services (EWS) code to delete the items or run an Office 365 content search to find the items and then remove them. There are two things to remember about using a content search to remove items. First, the actions supported by content searches only include soft deletes. Second, a content search can only remove 10 items at a time.

The Best Choice

If you need a clean mailbox as quickly as possible, the new mailbox approach might be best. If you want to keep the existing mailbox only but need it cleaned up as quickly as possible, Search-Mailbox is probably best used (assuming that your account has the necessary permissions).

But overall, if you want to impose control on a mailbox that tends to swell in terms of messages, use retention policies to keep a cap on what’s stored. Because it can apply policies to specific folders, an Exchange mailbox retention policy is more precise than an Office 365 retention policy.

——————————————-

For more information about mailbox management, read Chapter 6 of the Office 365 for IT Pros eBook. Retention policies and the Managed Folder Assistant are covered in Chapter 19.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.