Delete User Roles in bulk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 08:15 PM
Hi All,
I'm trying to delete the roles added by default to the users by using below script in the background but this is not working and not allowing to delete the records

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 08:44 PM
Hi @Vamshi_ch123 ,
You can't delete/remove roles which are inherited from the groups.
var userRole = new GlideRecord('sys_user_has_role');
userRole.addEncodedQuery("sys_updated_onON2024-05-29@javascript:gs.dateGenerate('2024-05-29','start')@javascript:gs.dateGenerate('2024-05-29','end')^inherited=False");
userRole.query();
userRole.deleteMultiple();
Here in encoded query I have added a condition to check are not inherited. This will remove user roles which are directly assigned to them.
However, if you want to remove the roles which are inherited (from group) you need to remove the user from that group.
If my answer helped you in any way, please mark it as helpful or correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 09:01 PM
Hi Sai,
it is showing inherited is true, how to delete these records then
Thanks & Regards
Vamshi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 09:30 PM
@Vamshi_ch123 @Query Group members (sys_user_grmrmber) table and remove/delete the user from that group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 08:49 PM
Hi @Vamshi_ch123 try below code
var userRole = new GlideRecord('sys_user_has_role');
userRole.addEncodedQuery("sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()");
userRole.query();
if (userRole.hasNext()) {
gs.print('Starting role removal process...');
while (userRole.next()) {
var sysId = userRole.sys_id.toString();
var deleteResult = userRole.deleteRecord();
if (deleteResult) {
gs.print('Deleted role assignment with sys_id: ' + sysId);
} else {
gs.print('Failed to delete role assignment with sys_id: ' + sysId);
}
}
gs.print('Role removal process completed.');
} else {
gs.print('No roles found for the specified date.');
}