Powershell script to delete items older than X days

Éric Moreau
Éric Moreau used Ask the Experts™
on
I have a lazy user who doesn't to empty his "deleted items" on a regular basis and doesn't want to use the "empty deleted items when closing Outlook" feature as he likes to delete older items and still keep some (ie keep last 2 months and delete everything older). This last trick wouldn't work if he is only using his phone/tablet for a while anyway.

Do you know which Powershell script I could use to delete emails from the "Deleted Items" older than 60 days?

I would schedule that script to run on a weekly basis and everybody would be happy.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Search-Mailbox -Identity $Username -SearchQuery "Received:<$('DD/MM/YYYY')" -DeleteContent

Open in new window

Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
thanks but I don't see anything that would limit to the "Deleted items" folder.

I have seen elsewhere that they are adding -Force after -DeleteContent. Why?
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
You can use retention policies/tags for that, but you have to create a custom one, as few years back Microsoft changed the default policy to never delete items from the Deleted Items folder.

Or, you can use a custom EWS-based script to clear the folder. Here's a sample one: https://www.sunilchauhan.info/2015/09/remove-all-emails-from-specific-folder_17.html
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
And by clear I mean remove items from specific date range :)
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Eric,

Did you want to run this on the Exchange server interacting with Exchange there, or on the users local computer interacting with Outlook there?


»bp

Commented:
The Force switch overrides the confirmation prompt displayed when your use the DeleteContent switch to permanently delete messages.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
Bill, my mails are hosted in Office 365.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
Vasil, isn't EWS deprecated?
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Okay, I can't help with that, good luck.


»bp
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
No, it isnt. And probably wont be deprecated anytime soon, there are at least dozen of migration apps out there that use it (including all tenant to tenant ones).
yo_beeDirector of Information Technology

Commented:
I like Vasil Retention Policy method.  
I am not using o365, but I have to think it is the same.  I am currently using a custom policy that Deletes older than 7 and move to recovery.  The recovery is set on the system level as the default one.  

https://docs.microsoft.com/en-us/exchange/recipients/user-mailboxes/deleted-item-retention-and-recoverable-items-quotas?view=exchserver-2019&viewFallbackFrom=exchonline-ww
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
yo_bee, I am full admin on my Office 365 tenant and I don't see all that details on the "mailbox usage" tab. I only see the last login and the usage. Nothing to adjust quota.
yo_beeDirector of Information Technology

Commented:
Retention policy  is a background process that runs constantly. If you apply a retention policy against a mailbox the emails will be tagged and based on your policy you the action will be applied. There is no set date to select.  It is all based of todays date and how old the item is.
https://docs.microsoft.com/en-us/office365/securitycompliance/retention-policies

Quota's are meant to cap users and warn them when they are close to the quota.  

I am running an older On-Prem exchange server and I have a custom policy called Trash Pickup for items older then 7 days.  This tags all deleted items to be deleted from the Deleted Folder, but does not permanently delete the items.  They will remain in the recovery bin for the default set time (14 days).  

This is what you need to do for this one mailbox or maybe make it a company wide policy.  

I use the analogy of your garbage pickup for your house.  You don't put items in the trash bin and expect to hold on to them forever.  It's called the trash bin for a reason. I explain if you have emails you want do not use the trash bin as a folder.  

This will not look like yours, but it will be the same principal to follow.
I am opening this up using EMC, but it can be done with Powershell as well.

I nagivgated to the Organization Config container and clicked on Mailbox
Click on the Retention Policy Tag tab to create a new policy
reten1.jpg
Configured the settings I desire
reten2.jpg
Clicked on the Retention Policy Tab and created a new policy to apply the tags to
reten3.jpg
Navigated to the Recipients Mailbox  and clicked on Mailbox Settings and selected Messaging Records Management
reten4.jpg
Checked Apply Retention Policy and browsed for the one I wish to apply
reten5.jpg
Selected the policy
reten6.jpg
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
I don't have the same thing in Office 365.

I am in the middle of writing some code on which I will have (hopefully) the full control over what I am deleting.
yo_beeDirector of Information Technology

Commented:
what about the link i posted in my last reply.  
What License of O 365 do you have? I am trying to find the comparison sheet
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
I have quickly checked the link. I have no retention policies set so far.

But I am currently testing the C# code I wrote based on Vasil/Sunil ideas. I will be able to have full flexibility over which mailboxes/folders/number of days that I want to keep.

I will surely be able to close the question later today.
yo_beeDirector of Information Technology

Commented:
Can you confirm what license you have for Office 365?
  1. E3
  2. Business Premimum
  3. Proplus
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
So finally I wrote some C# code using EWS as Vasil suggested and built a tool that will run on a weekly basis to clean exactly what I want.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
yo_bee, most users have business essentials, some have E1

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial