- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2019 07:06 AM
Hi ,
I have already old 600 inactive users .How to remove roles and Groups for those inactive users.I tried to create schedule job But it is not running .
Can you please suggest me .
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2019 07:14 AM
Hi,
You could use this in a background script to remove the groups.
var gr = new GlideRecord('sys_user');
var gr2 = new GlideRecord('sys_user_grmember');
gr.addQuery('active', false);
gr.query();
while (gr.next()) {
gr2.addQuery('user', gr.sys_id);
gr2.query();
while (gr2.next()) {
gr.deleteMultiple();
}
}
This would query for an inactive user, if found, query if they are a member of any groups, if found - delete all membership(s), then moves on to the next user. Keep in mind, this could run for a bit so be mindful when you're doing this.
Now if roles were assigned to users as well (which isn't best practice) ...then you can use this same script but then switchout sys_user_grmember with sys_user_has_role and run it again.
Please mark reply as Helpful/Correct, thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2019 07:14 AM
Hi,
You could use this in a background script to remove the groups.
var gr = new GlideRecord('sys_user');
var gr2 = new GlideRecord('sys_user_grmember');
gr.addQuery('active', false);
gr.query();
while (gr.next()) {
gr2.addQuery('user', gr.sys_id);
gr2.query();
while (gr2.next()) {
gr.deleteMultiple();
}
}
This would query for an inactive user, if found, query if they are a member of any groups, if found - delete all membership(s), then moves on to the next user. Keep in mind, this could run for a bit so be mindful when you're doing this.
Now if roles were assigned to users as well (which isn't best practice) ...then you can use this same script but then switchout sys_user_grmember with sys_user_has_role and run it again.
Please mark reply as Helpful/Correct, thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2019 05:54 PM
Please advise as how and where the background script can be run in ServiceNow so memberships is removed from inactive users ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2019 05:22 AM
Hello,
Search "background"....in your left-hand navigation within your ServiceNow instance and then use the script I posted above?
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2022 08:43 PM
Hi,
The line
while (gr2.next()) {
gr.deleteMultiple();
}
will delete all the inactive users from the sys_user table
Should be
gr2.deleteMultiple()
to delete all records the inactive user has in the sys_user_group table, no need for the while loop
or have I misunderstood?