Script to remove all members of a specific group.

northfdl54937
Tera Contributor

Hello,

I have a group called 'test_impersonator'.  After doing upgrades, patches, builds, i put multiple members in this group just to allow them to test as other users.

 

Once all testing is complete, i would like a script that i could run, that would automagically show me all the members of the group, and then delete all those members from the group.

 

I appreciate the assistance on the way this could be done!

1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @northfdl54937 

you will also directly remove the users from a group through Background - Scripts

var groupSysId = '0a52d3dcd7011200f2d224837e6103f2'; 

// Query the Group Members [sys_user_grmember] table to find members of the specified group
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', groupSysId);
grMember.query();

while (grMember.next()) {
    // Log the user’s Sys ID being removed - for audit purposes
    gs.info('Removing user with Sys ID: ' + grMember.user + ' from group: ' + groupSysId);
  
    // Delete the group member record
    grMember.deleteRecord();
}

gs.info('All members have been removed from the group with Sys ID: ' + groupSysId);

 

Note: Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma

View solution in original post

5 REPLIES 5

Is there any other way instead of deleting records?