- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
To reduce the ITOM License consumption, customer has asked to delete the AWS Sandbox Cloud Service Account CI's from the DynamoDB table but we are not able to delete any record.
PFB code created for this and help us if any modification is needed.
Code:
getCisFromServiceAccount();
function getCisFromServiceAccount() {
var gr = new GlideRecord('cmdb_ci_dynamodb_table');
gr.addEncodedQuery('nameLIKEsandbox');
gr.query();
while (gr.next()) {
var dbGR = new GlideRecord('sys_db_object');
dbGR.addQuery('label', gr.sys_class_name);
dbGR.query();
if (dbGR.next()) {
gs.info('Table Name from sys_db_object: ' + dbGR.name);
var ciGR = new GlideRecord(dbGR.name);
gs.info('Resource value: ' + gr.resource); // Added logging
if (ciGR.get(gr.resource)) { // Check if record is retrieved
gs.info('CI Name: ' + ciGR.name);
try {
ciGR.setWorkflow(false);
ciGR.deleteRecord();
gs.info('CI deleted: ' + ciGR.name);
} catch (e) {
gs.error('Error deleting CI: ' + e.message + ' - ' + e.stack);
}
} else {
gs.error('CI record not found with sys_id: ' + gr.resource);
}
} else {
gs.error('No sys_db_object found for class: ' + gr.sys_class_name);
}
}
gs.info('Total records in cmdb_ci_dynamodb_table: ' + gr.getRowCount());
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @MalathiP,
Your script fails because it unnecessarily queries the sys_db_object table and then tries to re-query the CI using ciGR.get(gr.resource). The .get() method requires a sys_id, and the resource field likely does not contain this value. The fix is to simplify the code and directly delete the record found in the initial query.
Try this script please:
// Query for the specific DynamoDB table CIs with 'sandbox' in the name
var gr = new GlideRecord('cmdb_ci_dynamodb_table');
gr.addEncodedQuery('nameLIKEsandbox');
gr.query();
var count = gr.getRowCount();
gs.info('Found ' + count + ' sandbox DynamoDB CIs to delete.');
// Loop through the found records and delete them
while (gr.next()) {
try {
var ciName = gr.getValue('name');
gs.info('Attempting to delete CI: ' + ciName + ' (sys_id: ' + gr.getUniqueValue() + ')');
// setWorkflow(false) prevents business rules or workflows from running on deletion
gr.setWorkflow(false);
// Delete the current record from the table
gr.deleteRecord();
gs.info('Successfully deleted CI: ' + ciName);
} catch (e) {
gs.error('Error deleting CI ' + ciName + ': ' + e.message);
}
}
gs.info('Deletion script complete.');
Hope this helps!
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution and helpful so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @MalathiP,
Your script fails because it unnecessarily queries the sys_db_object table and then tries to re-query the CI using ciGR.get(gr.resource). The .get() method requires a sys_id, and the resource field likely does not contain this value. The fix is to simplify the code and directly delete the record found in the initial query.
Try this script please:
// Query for the specific DynamoDB table CIs with 'sandbox' in the name
var gr = new GlideRecord('cmdb_ci_dynamodb_table');
gr.addEncodedQuery('nameLIKEsandbox');
gr.query();
var count = gr.getRowCount();
gs.info('Found ' + count + ' sandbox DynamoDB CIs to delete.');
// Loop through the found records and delete them
while (gr.next()) {
try {
var ciName = gr.getValue('name');
gs.info('Attempting to delete CI: ' + ciName + ' (sys_id: ' + gr.getUniqueValue() + ')');
// setWorkflow(false) prevents business rules or workflows from running on deletion
gr.setWorkflow(false);
// Delete the current record from the table
gr.deleteRecord();
gs.info('Successfully deleted CI: ' + ciName);
} catch (e) {
gs.error('Error deleting CI ' + ciName + ': ' + e.message);
}
}
gs.info('Deletion script complete.');
Hope this helps!
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution and helpful so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi Iftikhar,
Thanks a lot sharing the updated script. It actually deleted he DynamoDB table CI's with the name contains sandbox.
But my actual requirement is to delete the DynamoDB table CI's which are "Hosted on::Hosts" on AWS Datacenter and with the Cloud Service Account Name contains sandbox. PFA.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hello @MalathiP,
We have modified above script.
function deleteSandboxDynamoDBTables() {
var gr = new GlideRecord('cmdb_ci_dynamodb_table');
gr.addEncodedQuery('nameLIKEsandbox');
gr.query();
var count = 0;
while (gr.next()) {
// Optionally delete referenced CI if needed
// var referencedCI = gr.resource; // sys_id of referenced CI
// if (referencedCI) {
// var ciGR = new GlideRecord(gr.sys_class_name);
// if (ciGR.get(referencedCI)) {
// ciGR.setWorkflow(false);
// ciGR.deleteRecord();
// gs.info('Referenced CI deleted: ' + ciGR.name);
// }
// }
// Delete the DynamoDB table CI itself
gr.setWorkflow(false); // Skip business rules/workflows if needed
gr.autoSysFields(false); // Do not update audit fields
var name = gr.name + ''; // Save for logging
if (gr.deleteRecord()) {
count++;
gs.info('Deleted cmdb_ci_dynamodb_table record: ' + name);
} else {
gs.error('Failed to delete record: ' + name);
}
}
gs.info('Total records deleted from cmdb_ci_dynamodb_table: ' + count);
}
deleteSandboxDynamoDBTables();
If this helped to answer your query, please mark it helpful & accept the solution.
Thanks
Santosh.P
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago