- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2014 04:15 PM
We are using a test/sandbox instance and are very new to the platform. We have imported a large number of users, but now need to remove some of them. Please describe how I can bulk delete users based on the domain name of their email address (or other criteria if it is simple). We're looking to delete about 6500 users.
Thank you
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2014 08:30 PM
var user = new GlideRecord('sys_user');
user.addQuery('email','CONTAINS','enter_your domain');
user.setLimit('7000'); // Set limit so the query does not delete more users
user.query();
gs.log('Number of users to be deleted-->'+user.getRowCount());
while(user.next())
{
user.deleteRecord();
}
Check the above log before you actually delete the users to confirm if the query is working fine. Run the above script in background script.
Hope it helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2014 08:30 PM
var user = new GlideRecord('sys_user');
user.addQuery('email','CONTAINS','enter_your domain');
user.setLimit('7000'); // Set limit so the query does not delete more users
user.query();
gs.log('Number of users to be deleted-->'+user.getRowCount());
while(user.next())
{
user.deleteRecord();
}
Check the above log before you actually delete the users to confirm if the query is working fine. Run the above script in background script.
Hope it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2014 01:17 AM
You should be using deleteMultiple() rather than looping over each record one by one. Also do not use getRowCount() as this will iterate over every record too
var user = new GlideAggregate('sys_user');
user.addQuery('email','CONTAINS','enter_your domain');
user.addAggregate('COUNT');
user.query();
if (user.next()) {
gs.print(user.getAggregate('COUNT') + ' users will be deleted');
}
var user = new GlideRecord('sys_user');
user.addQuery('email','CONTAINS','enter_your domain');
user.setLimit('7000'); // Set limit so the query does not delete more users
while (user.next()) {
//user.deleteMultiple();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2014 01:42 PM
Andrew, I like this modification idea. I tried it first, but it didn't actually delete anything. It also didn't throw an error, so I'm not sure why it failed to work. Of course, I did remove the // .
Bahavesh's solution worked, but like you said, it took a very long time to delete all the records. Since this isn't a production instance, the added load is not a concern.
Thanks for the quick assistance.
Jeff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2014 01:46 PM
Jeff, Sorry can't believe i got that wrong before! Must have been half asleep!!
I should have wrote:
var user = new GlideRecord('sys_user');
user.addQuery('email','CONTAINS','enter_your domain');
user.setLimit('7000'); // Set limit so the query does not delete more users
user.deleteMultiple();
No need for the loop at all!
Andy