Exchange server admins often find themselves in situation of waiting to get rid of one or more mailboxes whether its routine cleanup of test leftovers or purging of trees no longer in use. Here, we shall look at one of the native Exchange Management Shell tools available at the disposal of an administrator to perform deletion.
Pre-requisites
- Make sure to double check whether the data being deleted is the right one.
- Make sure you have all the requisite roles assigned to perform deletion. To know what permissions you shall need, see the “Recipient Provisioning Permissions”.
- Disable accidental deletion protection to avoid – “You do not have sufficient privileges to delete…, or this object is protected from accidental deletion.” Error. This can be done via Exchange Management Shell as shown below:
Set-ADObject<user-name> -ProtectedFromAccidentalDeletion:$false
Remove-Mailbox
Syntax:Remove-Mailbox <MailboxIdentity>
Where <MailboxIdentity> can be email address associated with mailbox, its name or even alias.
It is also important to note that while this cmdlet marks the specified mailbox for purging, exchange retains the mailbox data till your organization’s retention period which is 30 days by default. You may use the purge switch -Permanent to permanently delete the mailbox. Though it is not recommended (as it impacts all users) you can also make use of Active Directory Service Interfaces Editor(ADSIEdit.msc) to remove single mailboxes.
You can also use the Get-DistributionGroupMember cmdlet to specify members of a distribution group that you want to delete
Get-DistributionGroupMember <name of a distribution group> | remove-mailbox
The below script allows you to delete inactive mailboxes up to 60 days
Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox,SharedMailbox | Where {(Get-MailboxStatistics $_.Identity).LastLogonTime -lt (Get-Date).AddDays(-90)} | Remove-Mailbox
Alternatively, you can also use -whatif switch with the aforementioned scripts to ensure you see what all mailboxes are going to get deleted if scripts are executed without -what if although mailboxes purged after retention period or permanently deleted cannot be recovered if you’ve a backup of exchange database (EDB) it will have the mailbox as its offline so retention policy doesn’t apply and can be used to restore the mailbox
Remove-StoreMailbox
This cmdlet can be used to permanently purge a mailbox of all its data resulting in irrevocable data loss. It can only be used on disconnected or soft-deleted mailboxes. There might be instances where one needs to get the disconnected mailboxes removed before their automatic purge at the end of retention period, for that – first get a list of all disconnected mailboxes using:
Get-MailboxDatabase | Get-MailboxStatistics | Where { $_.DisconnectReason -ne $null } | ft DisplayName,MailboxGuid,DisconnectDate,DisconnectReason
Now to permanently delete disconnected mailboxes, pipeline the results from above script into Remove-StoreMailbox cmdlet as shown:
Get-MailboxDatabase | Get-MailboxStatistics | Where {$_.DisconnectReason -ne $null} | ForEach {Remove-StoreMailbox -Database $_.database -Identity $_.MailboxGuid -MailboxState Disabled}
Cmdlet | Type of deletion | EDB retains the mailbox | Possibility of Mailbox Recovery | |
---|---|---|---|---|
Within retention period | After retention period | |||
Remove-Mailbox | Soft-deletion | YES | YES | No |
Remove-StoreMailbox | Permanent | No | No | No |
If you want an easier and intuitive way to recover lost mailboxes from your Exchange database backups you can always use EdbMails. EdbMails helps you extract the most data from even corrupt exchange databases. It also allows you to convert your exchange databases to PST for backup. You can also do live exchange or office365 migration and map individual folders to mailboxes of your liking with EdbMails.