Delete User records in bulk

PK14
Kilo Guru

Hi ServiceNow Expert,

I have a request to delete records from the user table where the account is inactive, the employee status is 'terminated', and the last login was before 2019. There are approximately 14,000 records that meet these criteria.I used the background script below, but it's taking over 2 to 4 hours to execute and does not seem to be deleting the records as expected. Could you please advise if anything is missing or suggest a better approach?

(function() {
    var gr = new GlideRecord('sys_user');
    gr.addQuery('u_employee_status', 'c1d616dc0f136100031a5d78a1050ea7');
    gr.addQuery('active', false);
    gr.addQuery('last_login', '<=', '2019-12-31 23:59:59'); 
    gr.addQuery('locked_out', true);
    gr.query();    
gs.log('Starting deletion of sys_user records. Count: ' + gr.getRowCount());
    
var count = 0;
   
 while (gr.next()) {

        gr.setWorkflow(false); 
        gr.autoSysFields(false); 
        gr.deleteRecord();
        count++;
    }    gs.log('Deletion complete. Total records deleted: ' + count);
})();

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@PK14 

there could be cascade effect and hence taking time.

It's only 14000 and should not take 2 to 4 hours

Try to use fix script and use setLimit(5000) and run 3/4 fix scripts in parallel.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

SupriyaWaghmode
Kilo Sage

@PK14 

1.Please use the fix-script - best approach. 

2.You can use set limit- for limited records and validate.

 

Please mark correct if this resolve your query.

 

--

Thanks & Regards ,
Supriya Waghmode |ServiceNow Consultant