With dozens of emails dropping in the mailbox every day, it is hard for a user to handle all those emails. Sometimes, bulk emails may come from a spammy marketing section, or phishing scams, that take up Outlook disk space. So, managing your Office 365 outlook storage space would be complicated and time-consuming. Typically to delete older emails from Outlook is the best strategy to boost the performance, storage, and work efficiency of Outlook.
But how will you manage the older emails in Outlook? There are multiple options, but which one will you pick to remove older emails from Outlook? Here in this blog, let’s learn how to quickly delete all Outlook emails older than X days.
Even if you delete emails from a specific folder in Outlook, the deleted items folder also accounts for Outlook storage. So, to permanently delete emails older than a set period in Outlook, use one of the below methods:
- Delete using the Auto-archive option in Outlook.
- Create an Outlook rule to delete emails older than a certain date from a specific folder.
- Using Retention policies in the Exchange admin center, create a rule that quickly deletes all Outlook emails older than X days.
When an admin wants to remove emails older than the stipulated number of days, they can perform by any of the below methods:
- Access another user’s mailbox and delete old emails using Auto-archive or create an outlook rule to delete emails from specific folders.
- Search for email messages and delete them using the Security and compliance center.
Eventually, working on these steps eats up a lot of your time! To make this even smoother, we developed a script that deletes bulk emails in one go!
Download Now: DeleteOlderEmails.ps1
This script uses the Search-Mailbox cmdlet and DeleteContent parameter to delete Outlook emails older than set period.
Search-Mailbox – Search for the particular mailbox and either copy the content to another delegated mailbox or delete the messages from the source mailbox. And when the Recovery Items folder hits the hard quota, it clears up the Recovery Items folder.
DeleteContent – Permanently deletes the older emails from the source mailbox, returned by the Search-Mailbox cmdlet.
In order to delete older emails from other mailboxes, you must have the Mailbox import-export role assigned. Otherwise, you cannot perform any actions.
At first, no user/groups are assigned to these roles. Admins must manually assign these role permissions. So, make sure you are assigned with the Mailbox Import-Export role.
You must Connect to Exchange Online PowerShell to assign the Mailbox Import-Export role. Then, run the following cmdlet, and this will eventually assign the mailbox import-export role to the user.
New-ManagementRoleAssignment –Role "Mailbox Import Export" –User "<AdminUPN>"
- The script uses modern authentication to connect to Exchange Online.
- The script can be executed with MFA enabled account too.
- Deletes the Outlook older emails by specific days.
- Removes the older emails from a specific user’s mailbox
- Deletes old emails of multiple users in Outlook using the import CSV file parameter.
- Generates a mail deletion report and log file.
- Automatically install the EXO V2 module (if not installed already) upon your confirmation.
- Credentials are passed as parameters (Scheduler–friendly)so be at ease! i.e., credentials can be passed as parameters rather than being saved inside the script.
- Exports the report result to a CSV file.
To run this script, choose any one of the below methods.
Method 1: You can run the script with both MFA and non-MFA accounts.
Method 2: To run a script non-interactively (i.e., Scheduler-friendly), use the below format.
.\DeleteOlderEmails.ps1 -UserName [email protected] -Password XXX
Running the script will prompt you to connect to Exchange online and ask you for the username and how many days you would like to run for. (Say, if you give 10 days, it will remove all messages older than 10 days from the respective user’s mailbox.)
This script exports a file with deletion specifications in a CSV format, and the output file is stored in the current working directory. The exported mail deletion report comprises the following attributes.
- Mailbox Name
- Deleted Mail Count
- Deleted Mail Size
Note: If an error occurs while deleting a mailbox, it will be notified in the log file.
Verification Log File:
Hereafter, you no longer have to worry about whether any error or crash happened while deleting the old emails in Office 365! Because this script generates a log file detailing the success and failure rates of the email deletion.
There can be an error in the log file if the CSV file contains an incorrect username, or if there is a slight network crash. With the log file, you can ensure whether the email deletion occurred successfully or not.
Our script supports in-built filtering parameters to delete emails from Outlook based on the requirement. They are listed below:
- Delete older emails of a specific user in Outlook
- Import a CSV file and remove Outlook emails older than X days.
This is another method of the general case (Normally, after running the script, it prompts you to enter the username). To remove emails in the outlook of a specific user, you can also mention the username as a parameter.
.\DeleteOlderEmails.ps1 -UserName [email protected] -Password XXX –UPN [email protected]
Let’s say you have to remove old emails of multiple users (like a team) from Outlook. In this case, you can import a CSV file of the usernames, and deletes all Outlook emails older than X days.
Note: This script doesn’t allow month counts (such as two months)- thus enter the days individually (30 days).
.\DeleteOlderEmails.ps1 -CSV “.\importusers.csv”
Import CSV File:
Import a CSV file with the UPN attribute containing the usernames.
When you import a CSV file to delete emails from the mailboxes, it exports two kinds of output files.
- Mail deletion report and
- Logfile for verification.
Finally, I hope this blog has shown you how to delete older emails in Outlook using PowerShell. Get rid of old emails and keep Outlook cluster-free with this script!
If you have any further questions, post them in the comments section. We will assist you!