Amit Gujarathi
Giga Sage
Giga Sage

Hi All,

 

Hope you are doing great.

In this article I will try to answer the most commonly asked question which is when and what should be used deleteRecord or deleteMultiple.

 

deleteRecord() and deleteMultiple

  • These two method are for the deletion of one or more records from database.
  • you should always make one thing clear in  mind that deletion of records in ServiceNow should be always be a rare scenario where deactivation is not an option.
  • Wherever deactivation is option you should go for the same.
  • Non of this function ask for any arguments as both of the record acts on the GlideRecord object.
  • deleteRecord deletes single record wehereas deleteMultiple delete all the records for the passed query
  • You should not use deleteMultiple when your table contains currency field instead you should loop through the records one by one to support deleteRecord(). 

Performance:

  • The difference is mainly in performance.

  • This has to do with the underlying SQL operations.

  • In SQL, when you want to change a record (including delete operations), the database does some commit checks on every record you make. If you run the operation on multiple records in a sequence, SQL will perform the same commit check but save time only once.

    So if you need to delete 5-10 records, it doesn't matter, if you need to delete 20000 records, deleteMultiple can save you a lot of time.

deleteRecord() vs deleteMultiple()

Entity deleteRecord() deleteMultiple()
Part of API Gliderecord GlideRecord
Arguments No No
Supports One record Multiple record matching passed query
ddot walking support Yes No
Exclude criteria NA If table contains currency
prior calls to setWorkflow() No Yes
Returns Flag that indicates whether the record was successfully deleted.
Possible values:
  • true: Record was deleted.
  • false: No record was found to delete.
Method does not return a value

 

Examples

 

deleteMultiple()

function deleteIncidents() {
var callerSysId = '<user sys id>'
  var gr = new GlideRecord('sc_cart_item');
  gr .addQuery('caller_id', callerSysId );
  gr .query();
  gr .deleteMultiple();
}

 

deleteRecord()

var rec = new GlideRecord('incident');
rec.addQuery('active',false);
rec.query();
while (rec.next()) { 
 gs.print('Inactive incident ' + rec.number + ' deleted');
 rec.deleteRecord();
}

 

Please be sure to bookmark this article as well as mark it as Helpful if you thought it was helpful.

 

Regards,

Amit Gujarathi

Technomonk Youtube 

Amit Gujarathi Linkedin 

TheTechnomonk.com 

ServiceNow Community Amit Gujarathi 

Comments
Daniel Oderbolz
Kilo Sage

Dear @Amit Gujarathi 
Thanks a lot for this valuable article. It's great that you mention the limitiation about Currency Field for deleteMultiple.

The performance difference has little to do with the database, it has to do with the architecture of ServiceNow.

The Loop you use when using deleteRecord Runs far away from the database (in a Java program that runs a Rhino instance, so at least two layers aboe the DB).

The checks you mention are also run there (if they are Business Rules).

So if you want to have a fast deletion (and you can "afford" integrity issues), you may turn off these checks by

 

// Turn off Business Rules 
rec.setWorkflow(false);
// Turn off Data Policies
rec.setUseEngines(false);

 

Best
Daniel

gustavo_and
Tera Contributor

Hi @Daniel Oderbolz @Amit Gujarathi,

Could you explain why there is a limitation with the deleteMultiple() method on records with currency fields?

 

I have a use case where the client wants to delete some of data in the CMDB since the data accuracy is questionnable and they want to start new processes with a clean CMDB.

 

I have tested deleting with a script initiating the cmdb_ci table and adding the encoded query corresponding what the client wants and then used deleteMultiple() method and there seems to be no issues. I may have missed something?

 

Can you please advise?

 

Thanks so much,

Gustavo

BalaK5
Tera Contributor

Hi @gustavo_and 

 

  • Currency fields in ServiceNow are complex and involve multiple related fields, such as the base currency, conversion rates, and potentially numerous currency amounts. deleteMultiple() does not handle the intricacies of these fields properly.
  • If there are any triggers, business rules, or logic that depend on the currency conversion or specific fields related to currency, deleteMultiple() might bypass these, leading to potential data integrity issues.

 

Version history
Last update:
‎12-07-2022 01:12 AM
Updated by:
Contributors