Unable to Delete Assets and Computers via Fix Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 02:23 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 05:26 PM - edited 03-28-2024 05:31 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2024 01:48 PM - edited 04-01-2024 01:59 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2024 04:58 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2024 08:16 PM
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