Unable to Delete Assets and Computers via Fix Script

syed_but_not
Tera Expert

HI, I'm attempting to delete all of our CI's that were brought in via our SCCM integration, I've tried the following code but it does not delete all the records. 

var ci = new GlideRecord('cmdb_ci');
ci.addEncodedQuery('discovery_source=SG-SCCM^sys_class_name=cmdb_ci_computer');
ci.query();
while(ci.next()){
	ci.deleteMultiple();
}

var computer = new GlideRecord('cmdb_ci_computer');
computer.addEncodedQuery('discovery_source=SG-SCCM');
computer.query();
while(computer.next()){
	computer.deleteMultiple();
}
var hardware = new GlideRecord('alm_hardware');
hardware.addEncodedQuery('ci.discovery_source=SG-SCCM');
hardware.query();
while(hardware.next()){
	hardware.deleteMultiple();
}

 

6 REPLIES 6

AshishKM
Kilo Patron
Kilo Patron

Hi @syed_but_not

 

You don't need to iterate while loop with deleteMultiple() method.

Try the updated one.

 

 

// delete all record from cmdb_ci_computer table
var computer = new GlideRecord('cmdb_ci_computer');
computer.addEncodedQuery('discovery_source=SG-SCCM');
computer.query();
computer.deleteMultiple();

// delete all record from alm_hardware table
var hardware = new GlideRecord('alm_hardware');
hardware.addEncodedQuery('ci.discovery_source=SG-SCCM');
hardware.query();
hardware.deleteMultiple();

/*
var ci = new GlideRecord('cmdb_ci');
ci.addEncodedQuery('discovery_source=SG-SCCM^sys_class_name=cmdb_ci_computer');
ci.query();
ci.deleteMultiple();
*/

 

 

Refer this for mass-deletion

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0717791

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hi, this does not work. I'm still getting the same error as before:

 

Error Message: Deleting RMP-FQQ4FK3 is not allowed because it is referenced in record Latitude 7430 within the Configuration Item table

you can check if there is any computer record with name "RMP-FQQ4FK3" has some reference value which is not allowing to delete it. you can exclude in query and manage manually.


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

@syed_but_not 

The error message you're encountering suggests that there are records referencing the CI you're trying to delete. Before deleting a CI, you need to ensure there are no references to it in other tables. This is to prevent data inconsistency and maintain data integrity within the ServiceNow instance.

 

// Delete referencing records first

// Example: Delete records from 'alm_asset'
var asset = new GlideRecord('alm_asset');
asset.addQuery('cmdb_ci', 'YOUR_CI_SYS_ID'); // Replace YOUR_CI_SYS_ID with the sys_id of the CI you want to delete
asset.query();
while (asset.next()) {
    asset.deleteRecord();
}

// Example: Delete records from 'alm_hardware'
var hardware = new GlideRecord('alm_hardware');
hardware.addQuery('ci', 'YOUR_CI_SYS_ID'); // Replace YOUR_CI_SYS_ID with the sys_id of the CI you want to delete
hardware.query();
while (hardware.next()) {
    hardware.deleteRecord();
}

// Continue with deleting CI records
var ci = new GlideRecord('cmdb_ci');
ci.addQuery('discovery_source', 'SG-SCCM');
ci.query();
while (ci.next()) {
    // Delete the CI record
    ci.deleteRecord();
}

// Delete cmdb_ci_computer records
var computer = new GlideRecord('cmdb_ci_computer');
computer.addQuery('discovery_source', 'SG-SCCM');
computer.query();
while (computer.next()) {
    // Delete the cmdb_ci_computer record
    computer.deleteRecord();
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks