How do BULK retire or delete asset records without deleting or retiring corresponding CI record

theroz
Tera Contributor

We have an ask to delete or retire asset records for virtual machines that have been accidentally created over some time. We need to delete or retire over 4000 records without affecting the corresponding virtual server CI. I know you can "delete asset only" from list view, but this is impractical for this many assets. So my question - Is there a way to bulk delete or retire assets without affecting the CI (leaving the CI intact)?

 

3 REPLIES 3

Community Alums
Not applicable
Hi Theroz, You can run the following script. I am not with a PC right now, so it's an untested script. var gr =new GlideRecord('alm_hardware'); gr.addEncodedQuery('PasteTheQueryHere'); gr.query(); gr.setWorkflow(false); //This will prevent deletion of CI records. while(gr.next()) { gr.deleteMultiple(); } Let me know in case of any issue. Cheers, Hardit Singh

AndyLock
Mega Guru

There should be an On Before business rule [Prevent deleting of non-terminated VM] for the VMWare CIs that prevents their deletion unless the state = ‘terminated’ and correlation_id is empty. It should be safe to delete the assets manually by an admin, and you'll get an error/info message telling you want what done with the CI (deleted or kept).

 

For a large amount of VMWare instances you can use this script:

var gr =new GlideRecord('alm_hardware');
gr.addEncodedQuery(‘model_category=38c9ada747613000880d1386ffde2707’);
gr.query();
gr.deleteMultiple();

I'd export the VMWare Instance Assets and CIs so you have an off-line record, just in case. 

 

akash_mehta
ServiceNow Employee
ServiceNow Employee

Theroz:  You should be able to use the same script include and function in a fix script to delete the asset only that the UI Action uses.

The UI Action script is:

var a = new AssetandCI();
a.deleteAssetOnly(current);

Your fix script could be something like this:

var aRec = new GlideRecord('alm_hardware');
aRec.addQuery('model','925c8736dbeb00100606e119139619fe');
aRec.query();
while(aRec.next()){
	var aDelete = new AssetandCI();
	aDelete.deleteAssetOnly(aRec);
}

 

Let me know if that works.  Please test in a development environment.

-Akash