How to delete messages in Exchange 2013

Welcome Forums General PowerShell Q&A How to delete messages in Exchange 2013

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #114414

    Participant
    Points: 6
    Rank: Member

    I was tasked with deleting email over a year old in the former owners' mailbox.

    I want to do it without taking full control of his mailbox.

    I thought this would do it, but it is doing nothing.

    Search-Mailbox -identity [USERNAME] -searchquery {(Received:> 01/01/2016 and kind:email)} -DeleteContent -Force

     

    I have tired it on his mailbox and mine, it does not delete anything.

     

    Any suggestions?

  • #114492

    Participant
    Points: 0
    Rank: Member

    Hi Tom,

    In order for query to work, spaces should be avoided as they are treated as free text. I see you have space between received and the value. And :> is not a valid operator. Please refer to this page:

    https://docs.microsoft.com/en-us/sharepoint/dev/general-development/keyword-query-language-kql-syntax-reference

     

    Also boolean operators should be written uppercase.

    so try with this:

    {Received>01/01/2016 AND kind:email}

     

    Regards,

    Dorian

  • #114870

    Participant
    Points: 6
    Rank: Member

    I do not think it is syntax at this point.
    I can do

    Search-Mailbox -identity "username" -searchquery subject:"test123" -deleteContent -Force
    
    Search-Mailbox -identity "tim taylor" -searchquery 'subject:"test123"' -deleteContent -Force
    

    no matter what I run, I get a Success: False

    I have confirmed I am a member of Organization Management, Import export, and Mailbox Import Export, and that Administrator is as well.
    I am also a member of Compliance Management, Delegated setup, Delivery Management, Help Desk, Public Folder Management, Recipient Management, Records Management, Server Management, and View-Only Organization Management.

  • #115053

    Participant
    Points: 319
    Helping Hand
    Rank: Contributor

    You have to be logged on with an Exchange admin account to run these commands successfully or an least start you PS session with that account.

    Are you even getting a response for just a lookup search first, vs trying a delete?

    https://docs.microsoft.com/en-us/previous-versions/exchange-server/exchangeserver-149/gg315525(v=exchsrvcs.149)

    $ExchangeAdmin = 'Administrator'
    $UserTarget = 'testuser'
    
    $MailParams = @{
        Identity      = $UserTarget
        TargetMailbox = $ExchangeAdmin
        SearchQuery   = 'Test EWS PoSH script'
        TargetFolder  = 'SearchLogs LogOnly'
        LogLevel      = 'Full'
    }
    Search-Mailbox @MailParams
    
    # Results of Search
    
    WARNING: The Search-Mailbox cmdlet returns up to 10000 results per mailbox if a search query is specified. To return more than 10000 results, use the
     New-MailboxSearch cmdlet or the In-Place eDiscovery & Hold console in the Exchange Administration Center.
    
    
    Identity         : contoso.com/LabUsers/TestUser 001
    TargetMailbox    : contoso.com/Users/Administrator
    Success          : True
    TargetFolder     : \SearchLogs LogOnly\TestUser 001-10/19/2018 8:36:34 PM
    ResultItemsCount : 2
    ResultItemsSize  : 76.61 KB (78,448 bytes)
    
  • #115152

    Participant
    Points: 17
    Rank: Member

    You can try using the range operator in your dates ".."
    Just make the starting date is something really old that you know will encompass everything:
    We'll also use -EstimateResultsOnly to preview if it is working and returning messages. You can change to delete after you can prove that you're getting a message count

    Search-Mailbox -identity [USERNAME] -searchquery "Received:01/01/2000..01/01/2016","Kind:email" -EstimateResultsOnly
  • #116655

    Participant
    Points: 6
    Rank: Member

    postanote,
    I changed it to match my environment, and success is still false.
    I copied the script to one of the exchange servers, logged in as Domain Admin to the exchange server, and ran it, same results.

    RunspaceId : [GUID}
    Identity : contoso.com/testuser
    TargetMailbox : contoso.com/Administrator
    Success : False
    TargetFolder : \SearchLogs LogOnly\
    ResultItemsCount : 0
    ResultItemsSize : 0 B (0 bytes)

  • #116661

    Participant
    Points: 6
    Rank: Member

    I Like Cheese
    I still get success of False, even logged directly into the exchange server as Domain Admin
    RunspaceId : GUID
    Identity : contoso.com/username
    TargetMailbox :
    Success : False
    TargetFolder :
    ResultItemsCount : 0
    ResultItemsSize : 0 B (0 bytes)

You must be logged in to reply to this topic.