Cleanup your Office 365 Inbox using Sweep and Retention Tags

Featured image


The Sweep feature is available for Office365 mailbox users in their portal. Using simple set of Sweep rules, users can delete or archive all email messages, or all but the most recent, from one or more senders. Also users can choose to block, delete, or archive future email messages from those senders.

For example, from inbox folder, if a message is selected and clicked the “Sweep” icon, actions will be displayed as shown in the below screenshot. To create new rule, click “View Rules”, then in the next screen you can create new rule to keep your inbox clean.

Retention Tag Retention tags are created and managed by admins to apply retention settings to folders and individual items such as e-mail messages and voice mail. These settings specify how long a message remains in a mailbox and the action to be taken when the message reaches the specified retention age. When a message reaches its retention age, it’s moved to the user’s In-Place Archive or deleted.

PowerShell Script to Cleanup Office365 Mailbox using Retention Tag

Step1: Create a new retention tag using “New-RetentionPolicyTag” cmdlet.

NOTE: Retention Tag Types supported - All (All folders of mailbox), Inbox, Deleted Items, Sent Items, Calendar, Clutter etc. Retention Actions supported - Delete and Allow Recovery, Permanently Delete, Move to Archive.

Step2: Create a new retention policy using “New-RetentionPolicy” cmdlet.

Step3:Apply the retention policy to Office365 mailbox users using “Set-Mailbox” cmdlet.

NOTE: After performing the above steps, MRM mailbox assistant processes the mailbox - messages are moved or deleted based on settings you configured in the retention tags.

Step4:To manually execute the MRM assistant on Office365 mailbox users, use “Start-ManagedFolderAssistant” cmdlet.

In this example, my requirement is to clean up the “Deleted Items” folder for Office 365 mailboxes, i.e., if their retention period (30 days) exceeded need to permanently delete the items in “Deleted Items” folder.

Following are the steps to achieve the above requirement,

1. Retention Tag Creation

$TagName = “DeletedItems_Retention”
$Type = DeletedItems
$AgeLimitForRetention = 30
$RetentionAction = PermanentlyDelete
New-RetentionPolicyTag –Name $TagName -Type $Type -RetentionEnabled $true -AgeLimitForRetention 
$AgeLimitForRetention -RetentionAction $RetentionAction

2. Retention Policy Creation

$PolicyName = “DeletedItems_RetenPolicy”
New-RetentionPolicy –Name $PolicyName -RetentionPolicyTagLinks $TagName

3. Applying Retention Policy to Office365 mailbox users

For Single Mailbox

Set-Mailbox “mailbox name” -RetentionPolicy $PolicyName

For all Mailboxes

Get-Mailbox | Set-Mailbox -RetentionPolicy $PolicyName

For Group Members

$GroupName = "MyDistributionGroup"
Get-DistributionGroupMember -Identity $GroupName | Set-Mailbox -RetentionPolicy $PolicyName

4.Manually executing MRM assistant on Office365 mailbox users

For Single Mailbox

Start-ManagedFolderAssistant -Identity “mailbox name”

For all Mailboxes

$mailboxes = Get-Mailbox  
$mailboxes | Foreach-Object{
$mailbox = $_
Start-ManagedFolderAssistant -Identity $mailbox.UserPrincipalName

For Group Members

$GroupName = "MyDistributionGroup"
$members = Get-DistributionGroupMember -Identity $GroupName
$members | Foreach-Object{
$member = $_
Start-ManagedFolderAssistant -Identity $member.Name