We have 1 lakh records in alm hardware table servicenow, i need to delete all but…
- User
We have 1 lakh records in alm hardware table servicenow, i need to delete all but background script taking time
- Assistant
If you need to delete a large number of records from the
alm_hardwaretable in ServiceNow and the operation is taking a long time, you can employ a more efficient approach. Deleting records in bulk can be resource-intensive, so it’s important to handle this task carefully to avoid performance issues. Here are a few strategies you can consider:1. Batch Processing
Instead of deleting all records at once, process the deletions in smaller batches. This reduces the load on the system and decreases the risk of timeouts or performance degradation.
// Set a limit for batch size var batchSize = 1000; var gr = new GlideRecord('alm_hardware'); gr.query(); var counter = 0; while (gr.next()) { gr.deleteRecord(); counter++; if (counter >= batchSize) { // Pause to prevent performance issues counter = 0; GlideSession.pause(1000); // Pause for 1 second // Allow for garbage collection and other background processes } }2. Scheduled Job
Create a scheduled job to delete records during off-peak hours when the system load is low. This can be done using the ServiceNow scheduler.
- Create UI Action or Script Include: Write a script that deletes records in batches.
- Schedule the Script: Use the Scheduled Jobs feature to run your script during a time window that minimizes impact on users.
3. Use a Background Script With Caution
If running from a background script, ensure you have necessary permissions and are aware of potential impact on system performance. Here is a refined example:
(function() { var gr = new GlideRecord('alm_hardware'); gr.query(); // Log total count gs.log('Total records to delete: ' + gr.getRowCount(), 'Record Deletion'); while (gr.next()) { gr.deleteRecord(); // Optionally log deletions gs.log('Deleted record: ' + gr.sys_id, 'Record Deletion'); } gs.log('Deletion completed successfully.', 'Record Deletion'); })();4. Removing Constraints
If constraints like
Business Rules,Auditing, orTriggersare slowing down the process, temporarily disable them, but ensure this does not violate compliance or business rules.5. Monitor for Errors
- Use logs (
gs.log()statements) to monitor the process and capture any errors or warnings. - Ensure that appropriate error handling is in place to handle script execution issues.
6. Validate and Backup Data
Before executing the deletion script, validate the criteria for deletion (use queries to list records) and consider backing up the data if it might be needed in the future.
Note:
Bulk deletions can impact other users and system processes, so proceed with caution and communicate to stakeholders as necessary. Always test scripts in a development environment before running them in production.
- User
Schedule job for table cleaner
- Assistant
Creating a scheduled job for table cleaning in ServiceNow is a good approach to handle routine maintenance tasks like deleting old or unnecessary records during off-peak hours. Here's a step-by-step guide on how to set up a scheduled job to delete records from a table like
alm_hardware.Steps to Create a Scheduled Job in ServiceNow
1. Create a Script Include or UI Action to Perform Deletion
First, write the script that will delete records from your table. This script can be encapsulated in a Script Include or a UI Action (though a Script Include is generally more reusable and manageable).
Script Include Example:
- Navigate to System Definition > Script Includes.
- Click New to create a new script include.
var TableCleaner = Class.create(); TableCleaner.prototype = { initialize: function() {}, cleanAlmHardwareTable: function() { var gr = new GlideRecord('alm_hardware'); gr.query(); var batchSize = 1000; var counter = 0; while (gr.next()) { gr.deleteRecord(); counter++; if (counter >= batchSize) { // Pause if needed counter = 0; GlideSession.pause(1000); // Pause for 1 second to reduce server load } } }, type: 'TableCleaner' };2. Create a Scheduled Script Execution
- Navigate to System Definition > Scheduled Jobs (or System Scheduler > Scheduled Jobs depending on your version).
- Click New to create a scheduled job.
- In the Name field, enter a descriptive name like "Alm Hardware Table Cleaner".
- Choose Run based on how frequently you want the job to run (e.g., daily, weekly).
- Set the Time or Repeat Interval for when the job should run.
- In the Script field, call your script include method:
var cleaner = new TableCleaner(); cleaner.cleanAlmHardwareTable();- Review other options as necessary (run as, time zone, etc.).
- Click Submit to create and activate the scheduled job.
Tips
- Testing: Always test your script thoroughly in a development or test environment before scheduling it in production.
- Logging: Add logging within your script include to monitor the deletion process and catch any errors.
- Backups: Ensure crucial data is backed up if needed, and confirm that deletion criteria are accurate to avoid unintentional data loss.
- Monitoring: Regularly monitor logs and system performance to ensure the scheduled job is running as expected without negatively impacting system performance.
- Optimization: Depending on the number of records and the size of each record in the table, customize your batch size and pause settings to find the optimal balance between speed and performance.
By following this setup, you can effectively manage large data deletions in ServiceNow through scheduled tasks, ensuring minimal disruption to system performance and user activities.
- User
Table cleanup wants to run
- Assistant
To set up a scheduled job for table cleanup in ServiceNow, you'll need to ensure that the job is configured and running correctly. Here’s a step-by-step guide to get it running effectively, ensuring that old or unnecessary records from a table (e.g.,
alm_hardware) are cleaned up at specified intervals.Steps to Run a Scheduled Table Cleanup
1. Ensure Script Include is Defined
Make sure you have created a Script Include to perform the actual deletion logic. Here's a simple example:
var TableCleanup = Class.create(); TableCleanup.prototype = { initialize: function() {}, cleanAlmHardwareRecords: function() { var gr = new GlideRecord('alm_hardware'); gr.addQuery('some_field', 'some_value'); // Modify as needed for your criteria gr.query(); var batchSize = 500; // Adjust based on performance considerations var counter = 0; while (gr.next()) { gr.deleteRecord(); counter++; if (counter >= batchSize) { counter = 0; // Pause to ensure system stability GlideSession.pause(1000); // 1 second pause } } gs.log('alm_hardware cleanup completed.', 'TableCleanup'); }, type: 'TableCleanup' };2. Set Up the Scheduled Job
-
Navigate to Scheduled Jobs:
- Go to System Scheduler > Scheduled Jobs > Script.
-
Create a New Scheduled Script Execution:
- Click New to create a new scheduled job.
-
Configure the Scheduled Job:
- Name: Enter a descriptive name, like "Alm Hardware Table Cleanup".
- Run: Choose how often to run the job (e.g., daily, weekly).
- Time: Set the time for the job to run, ideally during off-peak hours.
- Script: Enter the script to call your Script Include:
var cleaner = new TableCleanup(); cleaner.cleanAlmHardwareRecords(); -
Additional Configurations:
- Run as User: Decide whether to run it as a system admin or a specific user.
- Condition: Set any conditions that must be met for the script to run.
- Timezone: Specify the correct time zone for your scheduled execution.
-
Submit and Activate the Job:
- Click Submit to save the scheduled job.
- Ensure that the job is set to active so it runs according to the specified schedule.
Monitoring and Adjustment
- Logs: Use
gs.log()statements within your Script Include to log job execution and monitor any issues or successes in the system logs. - Testing: Test the job in a development instance to ensure it functions as expected and does not negatively impact performance.
- Database Performance: Monitor database performance and adjust your batch size or pause duration as needed.
- Backup: If possible, ensure data is backed up before deletion if there's any risk of data loss.
By following these steps, your scheduled job for table cleanup will run efficiently, helping to maintain optimal database performance by regularly removing unnecessary records. Always ensure that your cleanup criteria are accurate to prevent accidental loss of important data.
-