SharePoint doesn't allow to delete all 1000s of users at once. But, we can delete 400 at a time. That again is quite tedious - selecting 400 hundred check-boxes manually.
We can reduce the effort for selecting checkboxes manually by using the browser's developer tool.
Press F12 on your browser to open the developer tool.
Go to console and execute the following to select only 400 users at a time:
(function() {
var aa = document.querySelectorAll("input[type=checkbox]");
for (var i = 1; i < 400; i++){
aa[i].checked = true;
}
})()
Note: Loop is executed from the 2nd index to avoid 'select all' checkbox.
-----------------------------------------OR---------------------------------------------------
Use the following CSOM code:
public static void RemoveSitePermissions()
{
ClientContext ctxSource = new ClientContext("https://domain.com/sites/siteColl/siteName/");
string decryptedPwd = "password";
foreach (char c in decryptedPwd)
{
securePassword.AppendChar(c);
}
ctxSource.Credentials = new SharePointOnlineCredentials("user@domain.com", securePassword);
Web webSource = ctxSource.Web;
ctxSource.Load(webSource);
UserCollection userColl = webSource.SiteUsers;
ctxSource.Load(userColl);
ctxSource.ExecuteQuery();
foreach (User user in userColl.ToList())
{
try
{
userColl.Remove(user);
ctxSource.ExecuteQuery();
Console.Write(".");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}