Can you delete them manually?
I suspect they may be corrupt in some way and that command cant remove.
I assume it removed all the other items in the sent folder
search-mailbox with -deletecontent parameter not working correctly

Hi,
We have an Exchange Server 2013 and run the following command to export the email in a user's mailbox for a specified date range:
New-MailboxExportRequest -Mailbox <mailbox> -ContentFilter {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -FilePath "UNC-path\export.pst" -BadItemLimit 10
The emails in the mailbox are exported successfully.
We then run the following PowerShell command on the user's mailbox to delete all the emails within the date range:
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -deletecontent -force
All emails in that date range are deleted except for two emails that still remain in the sent folder of the user's mailbox.
What could be causing the two emails to not be deleted? How, can we ensure that all items in the mailbox within that date range are deleted?
Regards,
Exchange Server Management
Windows Server PowerShell
-
KyleXu-MSFT 25,746 Reputation points
2022-01-24T08:59:13.697+00:00 @Georges Carraha
It looks like they belong to sent mail rather than received mail.
If the suggestion below helps, please feel free to accept it as an answer to help more people.
Sign in to comment
4 answers
Sort by: Most helpful
-
Andy David - MVP 115.3K Reputation points • MVP
2022-01-20T11:23:28.257+00:00 -
Georges Carraha 1 Reputation point
2022-01-20T12:22:03.42+00:00 Hi,
The PowerShell command deleted all other emails in that folder within that date range. I am able also able to open those emails and delete them manually.
Andy David - MVP 115.3K Reputation points • MVP2022-01-20T12:31:38.043+00:00 weird. Ok, I have no explanation other than the command just barfed on those for some reason even though they matched the search criteria :(
Rich Matheisen 36,246 Reputation points2022-01-20T16:05:01.597+00:00 Would a message in the Sent Items folder necessarily have a "received" date??? Just asking.
Andy David - MVP 115.3K Reputation points • MVP2022-01-20T16:06:44.663+00:00 lol. Prb not! But I wonder why the others in that folder were deleted...
Georges Carraha 1 Reputation point2022-01-20T16:31:24.217+00:00 The command worked for the other emails in the sent folder. So I do not think that is the issue. However, I had also tried the command below and it made no difference:
search-mailbox -identity <identity> -searchquery {(sent -ge '01/01/2016') -and (sent -le '12/31/2018')} -deletecontent -force
Rich Matheisen 36,246 Reputation points2022-01-20T19:07:04.813+00:00 I haven't touched an Exchange server for almost eight years, so I'm not anywhere authoritative . . . however, if the help for the search-mailbox cmdlet is correct, it uses the KQL syntax for the "-SearchQuery" parameter's argument. If that's the case, shouldn't the query be something like this?
sent:(01/01/2016..12/31/2018)
Georges Carraha 1 Reputation point2022-01-21T17:09:02.327+00:00 Hi,
Both formats for the -SearchQuery are acceptable.
Regards,
Sign in to commentKyleXu-MSFT 25,746 Reputation points2022-01-21T07:09:38.67+00:00 You can also use the command below to check whether those two emails are contained in the filter:
The command below will return the number of emails that contained in this filter
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -EstimateResultOnly
If the result does not contain emails, it means you need to filter for those two emails.
One thing you need to notice: The Search-Mailbox cmdlet returns up to 10000 results per mailbox if a search query is specified, the emails that need to be deleted may exceed this limit.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.
-
Georges Carraha 1 Reputation point
2022-01-21T17:07:46.45+00:00 Hi,
I ran the command and it returned 0 bytes:
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '
12/31/2018')} -EstimateResultOnlyWARNING: 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.RunspaceId : 72c33216-eafd-4206-889e-1cd738fdfc30
Identity : <identity>
TargetMailbox :
Success : True
TargetFolder :
ResultItemsCount : 0
ResultItemsSize : 0 B (0 bytes)The strange thing is that I used the same date range in the filter to export the mailbox to a PST and the resulting PST contained ONLY those two emails. The command I used was below:
New-MailboxExportRequest -Mailbox <nailbox> -ContentFilter {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -FilePath "UNC-path\export.pst"
I'm not sure what other criteria I can use in the filter to remove just those two emails. It would not be a concern if it was only one users mailbox with the problem but there are several mailboxes with a similar issue. Identifying the folders with the emails that cannot be removed in each users mailbox will be time consuming and that is why I'm using the powershell script.
I inspected those two specific emails. When I open the properties, I see that the internet headers section is blank.
Regards,
Georges Carraha 1 Reputation point2022-01-24T14:31:52.207+00:00 Hi
In the filter I changed "received" to "sent". So when I ran the command below:
search-mailbox -identity <identity> -searchquery {(sent -ge '01/01/2016') -and (sent -le '12/31/2018')} -EstimateResultOnly
I get the result:
RunspaceId : 7ef555b0-1784-4ac0-bf05-f561ba0e3c65
Identity : : <identity>
TargetMailbox :
Success : True
TargetFolder :
ResultItemsCount : 5
ResultItemsSize : 2.744 MB (2,876,811 bytes)Regards,
KyleXu-MSFT 25,746 Reputation points2022-01-25T08:54:08.137+00:00 Then you could use the command below to export them to another mailbox, logon that mailbox you will know whether they are emails that you want:
search-mailbox -identity <identity> -searchquery {(sent -ge '01/01/2016') -and (sent -le '12/31/2018')} -TargetFolder test -TargetMailbox AnotherMailbox
Georges Carraha 1 Reputation point2022-01-26T19:36:20.267+00:00 Hi,
Exporting the emails is not the issue as stated in the original post. We have already done that with the command:
New-MailboxExportRequest -Mailbox <mailbox> -ContentFilter {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -FilePath "UNC-path\export.pst" -BadItemLimit 10
The problem is that we cannot delete all the emails within the date range specified using the command below:
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -deletecontent -force
Some emails get left behind.
Regards,
KyleXu-MSFT 25,746 Reputation points2022-01-27T09:17:29.05+00:00 Yes, I know. What I mean is to use the above command to detect whether there is an issue with the filter.
As you said, there return 0 email when you use the command below, it means that those two emails don't contained in this filter:
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -EstimateResultOnly
I inspected those two specific emails. When I open the properties, I see that the internet headers section is blank.
Based on this one, I guess those two emails are "sent" rather than "received", so, I suggest you use the second command to check whether those two emails are contained in this filter.
If you could find them when using "received", it is normal phenomenon that you cannot delete them when using "received" as filter.
Georges Carraha 1 Reputation point2022-01-27T14:12:31.897+00:00 Hi
You state "Based on this one, I guess those two emails are "sent" rather than "received", so, I suggest you use the second command to check whether those two emails are contained in this filter."
Which command are you referring to?
At the moment, if I run the command below it returns 0:
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -EstimateResultOnly
If I change "received" to "sent" in the filter, it also returns 0.
The command below:
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -deletecontent -force
gives the results:
Success : True ResultItemsCount : 0 ResultItemsSize : 0 B (0 bytes)
If I change "received" to "sent" in the filter, it also returns the same.
If I run the command below, it produces a PST file with ONLY the two emails in it.
New-MailboxExportRequest -Mailbox <mailbox> -ContentFilter {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -FilePath "UNC-path\export.pst" -BadItemLimit 10
If I change "received" to "sent" in the filter, it also produces a PST file with the same two emails in it.
This means that the filter does work when exporting using either -received or -sent in the search filter.
However, for some reason it does not work when deleting.
Regards,
KyleXu-MSFT 25,746 Reputation points2022-01-28T09:44:58.12+00:00 Here is a summary of this thread:
After running command below, there still exists two emails:
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -deletecontent -force
Then you said:
Based on this one, I guess those two emails are sent rather than received. Because received email should have Internet headers.After that, you reply to us, there exist 5 emails when check with "sent":
So, I suggest you use command to export them to another mailbox to check whether those two emails are contained in them.
But, you said this one (This is what I'm suspicious of):
One thing that also needs to pay attention to, the search-mailbox is using UTC time, if those emails are sent/received the morning of 01/01/2016, it may not be contained in this filter.
If this phenomenon only occurs on those two emails, I would suggest delete them manually directly.
Sign in to commentLimitless Technology 37,526 Reputation points2022-01-21T10:02:30.207+00:00 Hello GeorgesCarraha
I would recommend to check the next official article: https://learn.microsoft.com/en-us/exchange/troubleshoot/calendars/cannot-remove-items
Also check for a potential corruption: https://learn.microsoft.com/en-us/previous-versions/tn-archive/aa996773(v=exchg.65)?redirectedfrom=MSDN
and
In case the messages are related to large files:
https://social.technet.microsoft.com/Forums/en-US/b0107b72-13c7-412d-937e-a35194480226/unable-to-delete-large-messages?forum=exchangesvradminlegacy------------------------------------------------------------------------------------------------------------------------------------
--If the reply is helpful, please Upvote and Accept as answer--
-
Georges Carraha 1 Reputation point
2022-01-21T17:21:12.427+00:00 Hi,
The first article appears to apply to Exchange online. We are working on an on-premises Exchange server.
The third article does not apply because the messages are not large.
The second article may apply but doubt it. To check we will need to bring down the mail server for a while to do an integrity check.
One thing we found out is that the internet headers section for both emails are blank.
Maybe that is why the -searchquery filter in the powershell command below does not find anything:
search-mailbox -identity <identity> -searchquery {(received -ge '01/01/2016') -and (received -le '
12/31/2018')} -EstimateResultOnlyBut how would that explain the -ContentFilter filter in the powershell command below exporting the two emails:
New-MailboxExportRequest -Mailbox <nailbox> -ContentFilter {(received -ge '01/01/2016') -and (received -le '12/31/2018')} -FilePath "UNC-path\export.pst"
I find this so strange.
Regards,
Sign in to commentAndy David - MVP 115.3K Reputation points • MVP2022-01-21T17:10:04.107+00:00 I inspected those two specific emails. When I open the properties, I see that the internet headers section is blank.
and that would make sense if they were in the sent items... no headers for a sent item.
-
Georges Carraha 1 Reputation point
2022-01-21T17:23:41+00:00 Hi,
You are correct. I should have known better.
Regards,
Sign in to comment
-