Delete Users and Clean up User Information List

Some time back, I wrote an article on deleting user accounts from “User Information List” to get rid of the issue: Deleted accounts shown in People Picker: SharePoint People Picker shows deleted accounts in Active Directory and SharePoint

Since the people picker get data from both Active directory and “User Information List” of SharePoint (Which is a hidden list – SharePoint will create an entry in this list when the user added to the site or visits the site for the first time. SharePoint sync this list from user profile when user create/edit/update/delete items).

In my organization, for some reason, management decided to remove a particular domain completely. So the requirement turned towards SharePoint as: Remove All users of particular domain from User Information List, so that those accounts will not be visible in People picker! By the way, those accounts were already deleted from active directory and from SharePoint user profile import connections. So, I’ve to delete user from SharePoint user information list.

Now, I wish to generate a report with all the users from the User Information List, in order to identify and remove users of the particular domain from various site collections. Wrote the below code using Object Model before deleting user from user information list.

To Generate Report on All Users from User Information List:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace UserInformationListReport
{
    class Program
    {
        static void Main(string[] args)
        {
            string site;
            string EmailID=string.Empty;
            string UserName = string.Empty;

            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Enter the Web Application URL:");
                    site = Console.ReadLine();
                }
                else
                {
                    site = args[0];
                }

                //objects for the CSV file generation
                StreamWriter SW;
                SW = File.AppendText("c:\\UserInfoListReport.csv");
                //Write the CSV Header
                SW.WriteLine("Site collection Name, Site collection URL,  Account, User Name, E-Mail");

                SPSite tmpRoot = new SPSite(site);
                SPSiteCollection oSPSiteCollection = tmpRoot.WebApplication.Sites;

                //Enumerate through each site collection
                foreach (SPSite oSPSite in oSPSiteCollection)
                {
                  SPWeb oSPWeb = oSPSite.OpenWeb();
                  SPList oSPList = oSPWeb.SiteUserInfoList;

                      foreach (SPItem oSPItem in oSPList.Items)
                     { 
                      //For Deleted accounts, it may result NULL. So catch it.
                         try
                         {
                             // Set the variables
                            EmailID = oSPItem["Work e-mail"].ToString();
                            UserName = oSPItem["User name"].ToString();
                         }
                         catch (Exception ex2)
                         {
                             EmailID=string.Empty;
                             UserName = string.Empty;
                         }

                       //Log them to a file and delete - Field Names are case sensitive
                         SW.WriteLine(oSPWeb.Title.Replace(",", " ") + "," + oSPSite.Url + "," + oSPItem["Account"] + "," + UserName+ "," + EmailID);
                     }
                 }  

            //To Pause
            Console.WriteLine("Report has been Generated! Press a key to exit");
            Console.ReadLine();
            //Dispose of the bjects
            SW.Close();
            }

            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("User Information List Report", ex.Message);
            } 
        }
    }
}


To Remove users of a Particular Domain from SharePoint User Information List: (say the domain to be removed is: EMEA)

static void Main(string[] args)
        {
            string siteCollectionURL;
            string EmailID = string.Empty;

            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Enter the Site Collection URL:");
                    siteCollectionURL = Console.ReadLine();
                }
                else
                {
                    siteCollectionURL = args[0];
                }

            //objects for the CSV file generation
            StreamWriter SW;
            SW = File.AppendText("c:\\DeletedUsersFromUserInfoList.csv");
            //Write the CSV Header
            SW.WriteLine("Account, E-Mail");

            using(SPSite oSPSite=new SPSite(siteCollectionURL))
            {
                SPWeb oSPWeb = oSPSite.OpenWeb();
                SPQuery oSPQuery = new SPQuery();

                //Filter the EMEA Domain
                oSPQuery.Query = "<Where><Contains><FieldRef Name='Name' /><Value Type='Text'>EMEA</Value></Contains></Where>";
                SPList oSPList = oSPWeb.SiteUserInfoList;
                SPListItemCollection oSPListItemCollection = oSPList.GetItems(oSPQuery);
                
               /* To get the available Fields in the User Information List
                SPItem oSPItem = oSPList.Items[1];
                    for (int i = 0; i < oSPItem.Fields.Count; i++)
                    {
                    SW.WriteLine(oSPItem.Fields[i].Title + ",");
                    }
               */

                foreach (SPItem oSPItem in oSPListItemCollection)
                {
                    //For Deleted accounts, it may result NULL. 
                    try
                    {
                        // Set the variables
                        EmailID = oSPItem["Work e-mail"].ToString();
                    }
                    catch (Exception ex2)
                    {
                        EmailID = string.Empty;
                    }

                    //Log them to a file and delete - Field Names are case sensitive
                    SW.WriteLine(oSPItem["Account"] + "," + EmailID);

                    //Delete the User
                    oSPWeb.AllowUnsafeUpdates = true;
                    oSPWeb.SiteUsers.RemoveByID((int)oSPItem["ID"]);
                    oSPWeb.Update();
                    oSPWeb.AllowUnsafeUpdates = false;
                }
                
            }  

            //To Pause
            Console.WriteLine("Users deleted and Log has been Generated! Press a key to exit");
            Console.ReadLine();
            //Dispose of the bjects
            SW.Close();
            }

            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Remove From User Information List", ex.Message);
            }
        }

This code will delete all users from user information list in SharePoint 2010 (or in SharePoint 2007).

Delete Users from User Information List using SharePoint UI:
If there are only a few accounts to be deleted, then you can navigate to “Site actions >> Site settings >> All People (_layouts/people.aspx)” to remove users from the site collection. By the way, this application page pulls users from the user information list only!

Remove Deleted users from People Picker with PowerShell:
Here is the PowerShell script to remove deleted users from SharePoint people picker: SharePoint People Picker Showing Deleted Users? Remove with PowerShell

If you want to update existing profile in User Information List, You can use “Set-SPUser” cmd-let as in https://github.com/glapointe/PowerShell-SPCmdlets

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

8 thoughts on “Delete Users and Clean up User Information List

  • December 24, 2018 at 9:34 AM

    I think this site contains some real wonderful info for everyone :D.

    Reply
  • August 30, 2016 at 7:54 AM

    Deleting user from the user information list can cause many issues.

    Reply
    • August 30, 2016 at 12:53 PM

      You’ll loose the deleted user name in metadata/columns such as “Created by”, and in People picker columns.

      Reply
  • February 5, 2013 at 10:52 PM

    The Set-SPUser doesnt update all of the properties in the User Information List
    My script does, have a look http://www.softlanding.ca/Blog/Lists/Posts/Post.aspx?ID=46

    Reply
  • January 15, 2013 at 6:40 PM

    Hi there. This script looks really powerful, however, when I try to run it in powershell I get:

    The ‘using’ keyword is not supported in this version of the language.
    At C:UsersxxxxxxxDesktopUIList.ps1:2 char:6
    + using <<<< System; + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ReservedKeywordNotAllowed

    Reply
    • January 15, 2013 at 6:50 PM

      Oops! The code provided here is in .Net C#. and must be compiled in visual studio. its possible to rewrite this code in PowerShell also…

      Reply
  • November 30, 2012 at 10:24 AM

    Hi, thank you for this helpful post. I tried to use your code in Powershell to remove a Domain Group form User Information list but it generates following error.
    Exception calling “Delete” with “1” argument(s): “0x80070005”

    Reply
    • November 30, 2012 at 10:33 AM

      Looks like formatting issue in PowerShell. Try splitting your PowerShell code, and check!

      Reply

Leave a Reply