Background script to delete records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 01:17 PM
Hi All,
Have a requirement to delete empty rows from sys_user table. There are massive no of empty records based on conditions(encoded query)and when trying to delete its hanging.
Please suggest script to delete say 1000 or 2000 records from the list and then run the script again to delete next batch.
TIA

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 01:21 PM
You will have to come up with a query that works to identify your records and add it onto line 2. I would also suggest running this in sub production before you do this in a production environment.
var users = new GlideRecord("sys_user");
users.addEncodedQuery("query_here");
users.setLimit(1000);
users.query();
while(users.next()) {
users.deleteRecord();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 01:38 PM
@CV1 : Please try with the below code, and please make sure that you are executing in background from either background scripts or scripts. As we are dealing with deletion, please thoroughly test this out in sub-prod before running it in prod.
var userGr = new GlideRecord("sys_user");
userGr.addEncodedQuery("your_query_here");
userGr.setLimit(1000); // This limits your records
userGr.query();
while(userGr.next()) {
userGr.deleteRecord();
}
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 06:35 PM - edited 01-04-2024 06:35 PM
Hi @CV1 ,
You can use deleteMultiple() method after query() , don't need to iterate the query result set for bulk delete.
You can add the grUser.setLimit(1000), if record count are very huge else don't need it.
var grUser = new GlideRecord("sys_user");
grUser.addQuery("add query here");
grUser.query();
grUser.deleteMultiple(); // all result set record will delete in batch
-Thanks,
AshishKMishra
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 07:28 PM - edited 01-04-2024 07:29 PM
HI @CV1 That's a lot of records to delete. You can use on demand schedule job to delete the records This will ensure that system donot hang .
Alternatively use setLimit(100) in background script donot delete 1k records in one go browser might hung.
Harish