Sharepoint 2016 – MysiteCleanup Job functionality changes

Here is a Quick look at what's new  with the  Mysite Cleanup job in Sharepoint On-Prem  2016 &  how it impacts the  functionality of  profile deletion & cleanup .

How this worked in Sharepoint 2010 / Sharepoint 2013 ?

The user profile in the profile store can be marked for deletion (bdeleted=1) when the actual user is deleted or it meets a User filter in FIM (eg:UserAccountControl bitonequal 2). When the mysite clean up job is executed, such profiles are immediately deleted and corresponding mysites are kept for 14 days for data recovery.

More Info :

Note : While AD import is configured, you will need to execute a set of commands to get the disabled / deleted users marked for deletion

Ref: and

Whats new in Sharepoint 2016 ?

The SharePoint 2016 works similar to Sharepoint 2013 , however the profiles marked for deletion (bdeleted=1) will not be immediately deleted, such profiles will be preserved for 30 days.

This is how a profile look like when its active.






When the user is marked for deletion , the bdeleted and NTName values are updated. The NTName column value will be appended with the UserID.






To calculate the 30 days retention , we have introduced a new table (upa.userprofilescheduledforremoval) and the value will be populated when the user is marked for deletion. When the mysite Clean up job runs , it queries this table and picks up the profiles that are more than 30 days and process them.






Note : It is not recommended to Query any Sharepoint databases manually or to make changes other than ones described at .

I see the Profiles in my farm are never  Deleted , Even after 30 days & Successful Run of Mysite Cleanup Job ?

We  at Microsoft are already aware of this issue  &  working  to  get this addressed in one our upcoming updates .  Such profiles  can be removed manually, using Central Admin  or   PowerShell script as needed .  We will update this post once we have more info on the fix .


Update [Aug 9, 2017] : The above mentioned issue with MysiteCleanup job on Sharepoint 2016 has been resolved in Aug 2017 CU.

Download Sharepoint 2016 updates :

Please test , plan and review , back up the data and then install the CU on production farms.


POST By : Manjesh Menon [MSFT]

Comments (5)

  1. MP says:

    Thanks for the update . We have large no of disabled User profiles not getting deleted even after Running Mysite cleanup job in our SharePoint 2016 environment running MIM . Do you have PowerShell script somewhere to delete those till Microsoft releases the fix?

    1. Spses says:

      Hi MP,
      Below is the sample script that can be used to remove the profiles only. You may need to modify it to remove the Mysites if you wish to do so. You may need to prepare an input CSV file (Profiles_tobe_deleted.csv) copying the result of SQL query towards the profile DB “select ntname from upa.userprofile_full where bdeleted=1 (nolock)”. Its pretty important to have proper back up of the databases before you proceed.

      Disclaimer: Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that. You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.

      # Adding PS Snapin SharePoint
      $snapin = Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.SharePoint.Powershell’}
      if ($snapin -eq $null)
      Write-Host “Loading SharePoint Powershell Snapin” -ForegroundColor Green
      Add-PSSnapin “Microsoft.SharePoint.Powershell”

      # Importing the set of users.
      $fileName = “C:\Profiles_tobe_deleted.csv”;
      $inputFile = “”;

      $inputFile = Import-CSV $fileName -Delimiter ‘,’ -Header NTName
      Write-Error “Could not open file $fileName”

      # Delcaring the Sharepoint Variables.
      $site = new-object Microsoft.SharePoint.SPSite(“http://sps16app”);
      $ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
      $pm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

      # Declaring and creating the log files. Each time the script is executed, a new file will be created with the current time in the filename.
      $dateTime=Get-Date -format “dd-MMM-yyyy HH-mm-ss”
      $UPLogFile=”UserProfiles_Remove_bdeleted”+”_”+ $dateTime + “.log”

      $inputFile | Foreach-Object($_){


      $profile = $pm.GetUserProfile($User)
      $DisplayName = $profile.DisplayName
      Write-host “Current User:” $DisplayName
      $messageDisplayname = “Current User:” + $DisplayName
      Add-Content -Path $UPLogFile -Value $messageDisplayname

      $AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value
      Write-host “ID for the user :” $DisplayName “is ” $id
      $messageid = “ID for the user :” + $DisplayName + “is ” + $id
      Add-Content -Path $UPLogFile -Value $messageid

      Write-host “Removing the Profile..”
      $messageremove = “Removing the Profile..”
      Add-Content -Path $UPLogFile -Value $messageremove

      Write-host “Successfully Removed the Profile” $AccountName
      $messagesuccess = “Successfully Removed the Profile” + $AccountName
      Add-Content -Path $UPLogFile -Value $messagesuccess
      Add-Content -Path $UPLogFile -Value ” ”
      Write-host “Failed to remove the profile ” $AccountName
      $messagefail = “Failed to remove the profile ” + $AccountName
      Add-Content -Path $UPLogFile -Value $messagefail
      Add-Content -Path $UPLogFile -Value ” ”
      Write-host “Exception when handling the user ” $User
      $messageexcp = “Exception when handling the user ” + $User
      Add-Content -Path $UPLogFile -Value $messageexcp
      Add-Content -Path $UPLogFile -Value ” ”


  2. Hello Manjesh,
    Do the entries in the userprofilescheduledforremoval table get cleared when the user profile is re-imported? Does it prevent the deletion of mysites in such cases unlike in previous SharePoint versions when once marked for deletion, the mysite will remain queued for deletion and will be deleted no matter what.


    1. Hi Aneesh,
      As of today, our tests shows that the entry in the userprofilescheduledforremoval table will be removed or the value for the schedulestate will be updated once the user is imported back, This will prevent the mysite being deleted. We will further test a couple of scenarios once the above mentioned issue is completely addressed.

Skip to main content