Remove Deleted Users from Office 365 Permanently Using PowerShell

  author
Written By Mohit Jha
Anuraag Singh
Approved By Anuraag Singh
Published On September 3rd, 2022
Reading Time 4 Minutes Reading

Unable to remove deleted users from Office 365 permanently? You can not remove a user from deleted users in Office 365 Admin center. You need to run the PowerShell command for the same. In this article, we have described the step-by-step procedure for the same, just go through it & you will find your answer.

When you delete an Office 365 user, they are moved to the recycle bin for 30 days. These deleted users stay in the recycle bin for a retention period of 30 days set by Azure AD. In case the deletion was unintentional, then you can easily restore the deleted Office 365 user from the recycle bin during this retention period. However, after the retention period expires, the contacts cannot be restored anyhow.

In case you have deleted the contacts intentionally and want to delete them permanently by removing them from the recycle bin as well, you can do so with the help of Powershell.

Why Do You Need to Remove Deleted Users from Office 365 Permanently?

Consider a situation where you delete an Office 365 user, due to some issue in the display name of the contact. However, the user whose contact has been deleted needs to use the same UPN or SMTP address after removing the issue in the display name. Since the default retention period is 30 days, you will have to wait for 30 days, before you can use the same UPN for the user after making changes.

In such an instance, permanent deletion of contact from the Active Directory recycle bin is the only way, with the help of which, a user can create the new contact with the same UPN or SMPT address after making the required changes.

  • Wrong provisioning of an Office 365 user account.
  • Preventing a soft-match through Azure AD Connect when the UPN or primary SMTP address is the same.
  • An Office 365 user mailbox with an active hold is to be set to inactive (ex – for exiting employee).

Pre-Requisites

For proceeding with deleting the Office 365 contacts permanently, you need to download and install Azure Active Directory Powershell.

Procedure to Hard Delete Contacts from Office 365

Follow the below-mentioned steps for permanently deleting contacts from Office 365 account:

#1 Run Connect-MsolService for connecting to Azure Active Directory. Enter the admin credentials of the Office 365 account in the dialog box.

PS C:\Scripts> Connect-MsolService

#2  See the list of all the deleted contacts that are present in the recycle bin with the help of the below-mentioned command

PS C:\Scripts> Get-MsolUser –ReturnDeletedUsers

#3 Remove the contact by the following command

PowerShell Command to Remove Single User from Office 365 Permanently

PS C:\Scripts> Remove-MsolUser –UserPrincipalName [username] –RemoveFromRecycleBin 

Confirm
Continue with this operation?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

To remove all deleted users you can pipe the “Get-MsolUser output to Remove-MsolUser and add the -Force” switch to avoid being asked for each removal.

Caution: Be very careful here not to accidentally delete all users from your Azure Active Directory. As this process can be undone later.

Before deleting a user permanently, you should back up that Office 365 Mailbox locally. Moreover, you can easily download Office 365 emails to the desktop using the SysTools Office 365 Backup Tool.

PowerShell Command to Remove Bulk Deleted Users from Office 365 Permanently

PS C:\Scripts> Get-MsolUser –MaxResults 25 -ReturnDeletedUsers | Remove-MsolUser –RemoveFromRecycleBin -Force

Note: The MaxResults parameter used in the above command is a variable and hence can be changed depending upon the number of contacts that have to be deleted. Higher the number of contacts, the more time it will take to delete the contacts.

Conclusion

With the help of the above-mentioned PowerShell scripts, you can easily remove deleted users from Office 365 account permanently. This will help you in deleting the users rather than waiting for a period of 30 days to get them deleted automatically.

  author

By Mohit Jha

Mohit is a writer, researcher, and editor. Cyber ​​security and digital forensics are the two subjects that keep Mohit out of his seat. In addition, he hopes that the well-researched and thought-out articles he finds will help people learn.

Office 365 Backup Services