User roles inherited but not from group and roles are not manually added. unable to delete them

RahulRAJAS
Kilo Guru

Hi 
 I found an inactive user with roles but not added in any group or not manually added. all the roles say inherited true. Unable to delete them from sys user has role table and background script also didn't work.
how to deleted these roles ?

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@RahulRAJAS 

No scripting required

-> navigate to sys_user_has_role table

-> search for your inactive user

-> the Right Click Data Management -> Delete All with preview

AnkurBawiskar_0-1765793671895.png

this script in global scope in background script will work fine

(function() {
    var userId = 'sys_id_of_the_user'; // Replace with actual sys_id

    var roleGR = new GlideRecord('sys_user_has_role');
    roleGR.addQuery('user', userId);
    roleGR.query();

    while (roleGR.next()) {
        // Only process if the role is inherited
        if (roleGR.inherited == 'true') {
            roleGR.deleteRecord();
        }
    }
})();

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

tried both Ankur, but didn't work

@RahulRAJAS 

without you sharing the script and debugging results we can't help

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader