Mass deletion of Records from Computer Table(cmdb_ci_computer)

sharada_janagar
Kilo Explorer

Hi,

 

Could any one please guide me on how to delete all records from cmdb_ci_computer table using Glide Record query?( I have tried using "Delete all Records" By going to Tables and Columns but it is not deleting all records at once.)

 

Thanks and Regards,

Sharada.


1 ACCEPTED SOLUTION

harikrish_v
Mega Guru

HI Sharada,



A quicker way to do this would be use a direct SQL command to truncate the entire data, it will be done really fast and no record will be left behind in that table. Run the following command in the Scripts - Background:



gs.sql("truncate table <table_name>");



Hope this helps



Thanks & Regards,


Hari


View solution in original post

12 REPLIES 12

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Sharada,



Here is the script


var rec = new GlideRecord('cmdb_ci_computer');


rec.query();


while (rec.next()) {


gs.print('Inactive incident ' + rec.number + ' deleted');


rec.deleteRecord();


}



Thanks,


Pradeep Sharma


Thought I would just drop this here because I found this really useful in a similar situation.





doit("table_name");


//for example doit("cmdb_ci");



function doit(table) {


  var gr = new GlideRecord(table);


  //gr.addEncodedQuery("");


  gr.query();


  gr.deleteMultiple();


}


Hi Jeff,



That's a good catch - however there are a few caveats using that method, as documented in the Developer API Documentation:



https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=r_GlideRecord-deleteMultiple


deleteMultiple()

Deletes multiple records according to the current "where" clause.



This method does not delete attachments.


Dot-walking is not supported for this method. When using the deleteMultiple() function on referenced tables, all the records in the table will be deleted. Also, when using deleteRecord() to cascade delete, prior calls to setWorkflow() on the same GlideRecord object are ignored.


Do not use deleteMultiple() on tables with currency fields. Always delete each record individually.


To use the deleteMultiple() method in a scoped application, use the corresponding scoped method: deleteMultiple().


harikrish_v
Mega Guru

HI Sharada,



A quicker way to do this would be use a direct SQL command to truncate the entire data, it will be done really fast and no record will be left behind in that table. Run the following command in the Scripts - Background:



gs.sql("truncate table <table_name>");



Hope this helps



Thanks & Regards,


Hari