Write a script to delete all the inactive users who are the member of network group.

Akshita Jain
Tera Contributor
 
2 REPLIES 2

SanjivMeher
Kilo Patron
Kilo Patron

This is a script I generated from chatgpt. You can make changes to it and use it as required.

 

    // Define the group name
    var groupName = 'network';

    // Query the sys_user_group table to get the group details
    var groupGr = new GlideRecord('sys_user_group');
    groupGr.addQuery('name', groupName);
    groupGr.query();

    if (groupGr.next()) {
        // Get the group ID
        var groupId = groupGr.getUniqueValue();

        // Query the sys_user_grmember table to get the inactive users who are members of the group
        var memberGr = new GlideRecord('sys_user_grmember');
        memberGr.addQuery('group', groupId);
        memberGr.addQuery('user.active', false); // Filter inactive users
        memberGr.query();

        // Delete the inactive users from the group
        while (memberGr.next()) {
            memberGr.deleteRecord();
        }

        gs.info("Inactive users who were members of the '" + groupName + "' group have been deleted.");
    } else {
        gs.error("Group '" + groupName + "' not found.");
    }

 


Please mark this response as correct or helpful if it assisted you with your question.

Tony Chatfield1
Kilo Patron

Hi, unfortunately your requirement is not clear in your post, are you removing the users from their membership of the 'network' group? or deleting the sys_user records of users who are a member of the group but are inactive?
Note: normally it is not recommended that you delete reference data like sys_user records.