0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ServiceNow 任意のテーブルのレコードを全削除するbackground script

Posted at

USのシニアエンジニアと1年程一緒にアプリ開発をしていた中で使っていたものです。
テーブルのデータを削除したい場面で一括でレコードを消すスクリプトです。
自分の業務で必要な範囲でしか動作確認していません。
background scriptで範囲はglobalで実行です。

recursive_table_deletion ( "cmdb" );
delete_cmdb_discovery_tables();
function recursive_table_deletion ( base_table ){
  var cgr = new GlideRecord( "sys_db_object" );
  cgr.addQuery("name", base_table);
  cgr.query();
  if (cgr.next()) {
    var target_table = new GlideRecord( "sys_db_object" );
    target_table.addQuery("super_class", cgr.sys_id );
    target_table.query();
    while (target_table.next()) {
     var child = target_table.name ;
     gs.log ("Deleting from " + child );
     gs.truncateTable(child);
     recursive_table_deletion( child ) ;  
    }
  }
  gs.log("Truncate " + base_table);
  gs.truncateTable(base_table);
}
function delete_cmdb_discovery_tables() {
        // 消したいテーブル名をtablesのリストに入れる
	var tables = [
        "cmdb_software_instance", 
        "cmdb_ci_ip_address",
        "cmdb_ci_network_adapter",
        "cmdb_sam_sw_install",
        "cmdb_ci_cpkg",
	    "ast_license_software_instance",
	    "ast_license_package_instance",
	    "ast_software_license",
	    "ast_license_base", 
	    "cmdb_rel_ci", 
	    "cmdb_serial_number", 
	    "cmdb_pid", 
	    "ecc_agent_ci",
	    "discovery_printer_supplies",
	    "cmdb_switch_port",
	    "cmdb_discovery",
	    "cmdb_tcp_connection",
	    "cmdb_tcp_half",
	    "cmdb_tcp",
	    "cmdb_running_process",
	    "discovery_device_history",
	    "discovery_status",
        "discovery_result",
        "automation_error_discovery_category_stats",
        "automation_error_discovery_code_stats",
        "automation_error_discovery_msg",
        "discovery_status_log",
	    "discovery_switch_bridge_port_table",
	    "discovery_switch_fwd_table",
	    "sys_object_source",
	    "vcenter_datastore_hostmount",
	    "discovery_switch_spanning_tree_table"
	];
	for (var i in tables) {
	    var table = tables[i];
	    var gr = new GlideRecord(table);
	    if (!gr.isValid())
	        continue;
	    gs.log("Truncate " + table);
		gs.truncateTable(table);
	}
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?