Purge deleted users in Office365
When we delete a user in Office365 this isn´t deleted as well because it gets sent to a recycle binfor 30 days (Soft-Delete). This can be good when we want to restore it with all the permissions, content and details, but what happens when the user John Smith leaves the company and some days further another user named Judy Smith gets incorporated to the company as well? If we would like to assign the new username following the org policy Initial.LastName (J.Smith) for example, we will encounter that Office365 will Return an error saying it cannot create the user because the username already exists.
To force the deletion of the user from the recycle bin, we must do it with the use of PowerShell.
To do so, we must get our environment prepared for PowerShell and Office365.
First we must signin to our Office365 subscription with the use of the following CmdLets:
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Import the Online Services module:
Import-Module MSOnline
Run the following CmdLet to get the ObjectId of the user to be deleted:
Get-MsolUser -All -ReturnDeletedUsers|select userprincipalname,objectid
Finally execute the purge of the desired user with the ObjectId obtained before:
Remove-MsolUser -ObjectId «objectid» -RemoveFromRecycleBin -Force
Hope it helps someone.