- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2025 05:47 AM
How can we remove users from a group if they have been inactive for a specific period, such as 2 weeks?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2025 06:00 AM
Hello @Sakshi Shukla
You can try below scheduled job code:
(function executeScheduleJob() {
var twoWeeksAgo = new GlideDateTime();
twoWeeksAgo.addDaysUTC(-14);
var inactiveUsers = new GlideRecord('sys_user');
inactiveUsers.addQuery('active', false);
inactiveUsers.addQuery('sys_updated_on', '<=', twoWeeksAgo);
inactiveUsers.query();
gs.info('count' + inactiveUsers.getRowCount());
while (inactiveUsers.next()) {
var groupMemberships = new GlideRecord('sys_user_grmember');
groupMemberships.addQuery('user', inactiveUsers.sys_id);
groupMemberships.query();
while (groupMemberships.next()) {
groupMemberships.deleteRecord();
}
}
})();
Let me know if the above code works for you.
Thank you!
Thank You!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2025 06:01 AM
You can create a Flow that runs on a daily basis with the following logic:
Trigger: Scheduled to run daily
Action: Update records
Steps:
-
Query the Group Member table
-
Lookup users who are inactive
-
Update the Group accordingly (e.g., remove or flag inactive users)"
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2025 06:02 AM
Hi @Sakshi Shukla ,
You can create schedule job with condition on last login time.
Chandan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2025 06:09 AM
Hello @Sakshi Shukla,
you can try below scheduled job which runs daily/ weekly as per your requirement -
(function() {
var fourteenDaysAgo = gs.daysAgoStart(14);
// Get all inactive users (active = false) who were last updated 14 days ago or more
var inactiveUsers = new GlideRecord('sys_user');
inactiveUsers.addQuery('active', false);
inactiveUsers.addQuery('sys_updated_on', '<=', fourteenDaysAgo);
inactiveUsers.query();
while (inactiveUsers.next()) {
var userId = inactiveUsers.sys_id;
// Get all group memberships for this inactive user
var groupMembers = new GlideRecord('sys_user_grmember');
groupMembers.addQuery('user', userId);
groupMembers.query();
if (groupMembers.hasNext()) {
while (groupMembers.next()) {
groupMembers.deleteRecord(); // Delete this specific group membership
}
}
}
})();
Let me know if this not works for you.
Please mark this response as Correct and Helpful if it assists you.
Thanks,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2025 06:40 AM
Hello @Sakshi Shukla,
If you feel my answer is also works for you then please mark this as correct. You can accept multiple solutions as correct.
Thanks,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2025 06:24 AM
Just go through this link and use Data Management Delete jobs, It will also track when the job ran and deleted records.
Thanks
Deepak