Exchange Online bulk delete messages specific folder

Anna Mantica 21 Reputation points
2021-02-08T19:15:43.49+00:00

Using Office 365 and I have a shared mailbox that is out of control and the sent items need to be bulk deleted based on date range. The old search-mailbox has been deprecated so I am unable to figure out how to accomplish the command below using powershell v2.

search-mailbox -identity <usermailbox> -searchquery {received:mm/dd/yyyy..mm/dd/yyyy} -deletecontent

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,634 questions
{count} votes

Accepted answer
  1. Yuki Sun-MSFT 41,171 Reputation points Microsoft Vendor
    2021-02-09T06:26:51.967+00:00

    Hi @Anna Mantica ,

    I've just checked in my test tenant and it seems to me that the Search-Mailbox cmdlet still works.

    According to this link, "only the Mailbox Import Export role gives you access to the DeleteContent parameter", so please first make sure you have the required permission. If the Mailbox Import Export role hasn't been assigned to your account, you may run the following command to add it:

    New-ManagementRoleAssignment -Role "Mailbox Import Export" -User <youraccount>  
    

    65605-1.png
    Reconnect to the Exchange Online Powershell for the added permission to take effect.

    Then as regards to the script syntax, as mentioned by Andy, it's not feasible to search on just the Sent Items folder, so as a workaround, I would try adding more condition in the the search query part like searching for items sent from the mailbox itself:

    Search-Mailbox user1 -SearchQuery {Received:"12/1/2020..2/9/2021" AND from:user1@contoso.com} -DeleteContent  
    

    65681-2.png
    I tested in my lab using the syntax above and it worked at my end. But please be aware that the drawback is if user1 was included in the recipients list of a message he sent, the mail in his Inbox will also be deleted.

    Furthermore, before using the -DeleteContentthe parameter, it's always recommended to estimate the search results using the -EstimateResultOnly parameter, or you can copy the search results to a target mailbox to take a look at:

    Search-Mailbox user1 -SearchQuery {Received:"12/1/2020..2/9/2021" AND from:user1@contoso.com} -EstimateResultOnly  
    Search-Mailbox user1 -SearchQuery {Received:"12/1/2020..2/9/2021" AND from:user1@contoso.com} -TargetMailbox <targetmailbox> -TargetFolder TEST -LogLevel Full  
    

    In addition, another thought as per your requirement is to use the Advanced Find feature in Outlook:

    1. Given yourself the Full Access permission to the shared mailbox.
    2. Add the shared mailbox as a separate account in Outlook(Refer to the Method 1 in this link.)
      Please Note: Since the web site is not hosted by Microsoft, the link may change without notice. Microsoft does not guarantee the accuracy of this information.
    3. Open Advanced Find by pressing Ctrl+Shift+F, click Browse, select the Sent Items folder, Click Advanced, set the criteria as below, click Find Now:
      65691-3.png
    4. When the search results show up, choose one of the results, press Ctrl+A to select all of the items in the results, press Shift+Delete then choose Yes to permanently delete all of them at once.

    Hope you can find this helpful.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anna Mantica 21 Reputation points
    2021-02-08T19:19:19.203+00:00

    That doesn't work. It only deletes messages 10 at a time. I have thousands that need to be deleted.


  2. Anna Mantica 21 Reputation points
    2021-02-08T19:30:12.797+00:00

    Way too many emails to delete via Outlook or OWA. I'd prefer to be able to just bulk delete as opposed to creating a new policy. I have 3 other mailboxes that need to be cleaned up as well but different folders within each mailbox. (I inherited this mess, I would not have let these mailboxes get this far out of control if I had setup this organization).

    0 comments No comments

  3. Andy David - MVP 150.6K Reputation points MVP
    2021-02-08T19:39:16.563+00:00

    Ok, Understood. :) Last I checked, search-mailbox still works in Exchange Online, so even though deprecated, it should still work:

    Search-Mailbox -identity <usermailbox> -SearchQuery {Received:1-May-2016..1-Aug-2020} -DeleteContent  
    

    Make sure you have the correct perms and log first just to ensure it looks right. Consider copying to another mailbox to cover yourself just in case...

    65459-image.png

    https://learn.microsoft.com/en-us/exchange/search-for-and-delete-messages-exchange-2013-help

    0 comments No comments

  4. Anna Mantica 21 Reputation points
    2021-02-08T19:42:27.343+00:00

    I get an error when I try running that:

    Search-Mailbox : The term 'Search-Mailbox' is not recognized as the name of a cmdlet, function, script file, or
    operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
    again.
    At line:1 char:1

    • Search-Mailbox -identity Office -SearchQuery {Sent:1-Jan-2013..1-Dec- ...
    • ~~~~~~~~~~~~~~
    • CategoryInfo : ObjectNotFound: (Search-Mailbox:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.