The organization where I work as a Salesforce developer decided to upgrade a system according to Salesforce’s new update. During the upgrade, they wanted to deactivate users with specific profiles to ensure data security. I was assigned the task of deactivating all users with a particular profile.
Here, I will explain how to activate or deactivate a user from Salesforce Apex. Then, we will see how to deactivate or activate multiple users using SOQL in Apex.
Deactivate the User From Salesforce Apex
In the following program, I explained how to deactivate any user by User ID in Salesforce Apex. Here, you can see I have one activated user. We will deactivate this user using the Apex class in Salesforce.

We can deactivate users using the Apex class in Salesforce. For that, we need the User ID of the user we want to deactivate. Then, by creating an SOQL query, we can find out whether this user is active. If the user is active, then deactivate that user and update the user record.
- ID userID: I created a variable with ID as the data type and declared the user ID I want to deactivate.
- User user: Then I created a ‘user‘ variable with User as a data type to store information that we will fetch from the User object.
- If Condition: It will check whether the user who will be deactivated is currently active.
- Else: You will get the message that you entered in the else condition in the debug log.
If it is activated, then deactivate it and update the user record.
public class DeactivateUser {
public void DeactiveUser() {
ID userID = '005J3000000h8DlIAI';
User user= [SELECT Id, Name, IsActive FROM User WHERE Id = :userID];
if (user.IsActive=true) {
user.IsActive = false;
update user;
System.debug(+user.Name + 'User is Deactivated');
}
else {
system.debug('User is not Deactivated');
}
}
}Now, we need to execute the program to open an anonymous window. Then, you need to create an object and an instance of the class.
Syntax: Class_Name instance = new Class_Name(); -> instance.Method_Name();
Then click the Execute button to execute the Apex class.
DeactivateUser d = new DeactivateUser();
d.DeactiveUser();
After executing the Apex code, navigate to the user details and refresh the page. You will see that the user has been deactivated.

In this way, we can deactivate the user using the Apex class in Salesforce.
Activate the User From Salesforce Apex
Now, when you want to activate a deactivated user, you also need to fetch the user’s details and check whether the user is deactivated. I explained this in the above program.
Here, we need to check the IF condition. If the user is deactivated, we need to make it active and update the user record. Otherwise, we need to display an already active message.
if (user.IsActive=false) {
user.IsActive = true;
update user;
System.debug( +user.Name + 'User is Activated' );
}
else {
system.debug( 'User is already Active' );
} In this way, we can activate the deactivated user from Salesforce Apex using the user ID.
Deactivate Multiple Users Using Salesforce Apex
In the above apex program, we have seen the deactivation of particular users by UserId. Now, we will see how to deactivate multiple users from Salesforce Apex using SOQL.
In the following image, you can see the maximum number of active Identity user profiles. We will create a class to deactivate all identity user profiles.

Here, I created an Apex class with a method. In that method, I created a list to store the list identity profile user records, and using SOQL, I retrieved user records and stored them in the userlist variable.
- SOQL: In SOQL, I used the WHERE clause to retrieve specific types of records.
- for loop: In a for loop, we need to create a User object instance and pass the retrieved active user list to the object instance. With this, we will check whether users in the list are active; if they are active, then deactivate all users.
Then, Update the userlist to save the deactivated user in the User object.
public class DeactivateMultipleUsers {
public void deactivateUsers(){
List<User> userslist = [Select ID, Name, IsActive From User WHERE Profile.Name = 'Identity User'];
for(User user : userslist){
if(user.IsActive = true){
user.IsActive = false;
}
}
UPDATE userslist;
system.debug('Identity Users has been Deactivated successfully');
}
}
After executing the Apex code, navigate to the user list view and refresh the page. You will see that all users with the Identity User profile have been deactivated.

Activate Multiple User From Salesforce Apex
Now, when we want to activate multiple users, we also need to create a list of records with specific conditions. Again, using a for loop, the loop will iterate through the user record list to check whether the users present in the list are deactive or not.
If the user is deactivated, then make it active, and last, Update the user record list.
public void ActivateAllUsersAgain(){
List<User> userslist = [Select ID, Name, IsActive From User WHERE Profile.Name = 'Identity User'];
for(User user : userslist){
if(user.IsActive = false){
user.IsActive = true;
}
}
UPDATE userslist;
system.debug('Identity Users has been reactivated successfully');
}In this way, we can activate multiple users by retrieving the user record from Salesforce Apex.
Automatically Deactivate Users Using Salesforce Apex
For example, if users didn’t log in to a Salesforce account more than five days ago, they should be deactivated.
Here, we have criteria for users who have not logged in to their Salesforce account in the last five days. Using LastLoginDate, we can calculate the days since his last login, and then we can deactivate the user automatically based on that criterion.
Here, I created a class with the AutoDeactivateUsers() method. In this method, we will get the date five days before that date. I stored it in the FiveDaysBeforeDate variable, which is available with the DateTime data type.
List<User>
For loop: In a for loop, we can check the activated user records and deactivate them. Finally, we can Update the user list.
Then, open the anonymous window and execute the code to deactivate the users who haven’t logged in in the last five days. If we do it manually, we have to run the code multiple times. To avoid that, we have a class schedulable apex, and by using that, we can automate this process.
public class AutoDeactiveUsers {
public void AutoDeactivateUsers(){
DateTime FiveDaysBeforeDate = Date.Today()-5;
List<User> userList = [ Select ID, Name, IsActive, LastLoginDate From User Where
LastLoginDate <= :FiveDaysBeforeDate ];
for( User user : userList ) {
if ( user.IsActive = true ) {
user.IsActive = false;
}
}
Update userList;
}
}Create a Scheduled Apex Class in Salesforce
To automatically run the AutoDeactivateUsers() method, we need to create a scheduled Apex class in Salesforce. This allows us to schedule an Apex class to run at specific times or intervals, which helps automate tasks like deactivating inactive users.
To create the scheduled class, we need to enter the global access specifier instead of the public, followed by the class name. As Schedulable is the interface, we need to implements it into this class.
Then, create a method with global. When we create the scheduled class, we must create an execute() method. Then, in that method, we need to create an object of the class to call the method from the previously defined class and, using that object, call the method that you want to execute from the previous class(AutoDeactiveUsers).
Now we need to schedule this class. For that, we need to navigate to the Salesforce org.
global class ScheduleDeactiveUserClass implements Schedulable {
global void execute ( SchedulableContext sc ) {
AutoDeactiveUsers obj = new AutoDeactiveUsers();
obj.AutoDeactivateUsers();
}
}On the Home Page, in the Quick Find Box, search for the Apex Classes and click on it. Then click the Schedule Apex button.

Now, enter the Job Name as per your requirement and click the lookup icon to select the Apex Class.

As you click the lookup icon on the Apex Class field, you will see the Schedule Classes that you created in the developer console. Here, you need to select the class that you created to schedule any process.

Then, in the Schedule Apex Execution, you need to set the frequency, start date, end date, and preferred time to trigger the scheduled class so that the process you created in the public class will get executed at that time.
Then click the Save button.

In this way, you can create a public class to create a program for deactivating users and then automate this process instead of manually running that class. We have scheduled a class in Apex so that we can schedule the process.
Conclusion
In this tutorial, I explained how to activate or deactivate a user from Salesforce Apex. I explained how to deactivate a user by passing a specific user ID to a variable, and again, how we can make that user active.
Then, we saw how to deactivate or activate multiple users using SOQL in Apex. In that, I explained how to create a list of users with specific criteria and then how to use each loop to iterate over the list of users and deactivate them.
You may like to read:
- Get Current User Info Using Apex in Salesforce
- Variables and Data Types in Salesforce Apex
- Create a Set Collection in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.