Over time, Task records that were never started—and have been sitting in your account for years—can accumulate and clutter your workspace. This makes it harder to locate current tasks and may even affect system performance.
As a NetSuite Administrator, maintaining clean and relevant data is essential. One effective approach is to automatically remove obsolete Task records based on age and status.
This article walks you through how to use a SuiteScript 2.x Scheduled Script to automatically delete all “Not Started” tasks with a start date older than four years, complete with batch processing and audit logging.
Example Scheduled Script: Batch Deletion of Aged “Not Started” Tasks
/**
* @NApiVersion 2 .x
* @NScriptType ScheduledScript
*/
define(['N/record', 'N/search', 'N/log'], function(record, search, log) {
const RECORD_TYPE = record.Type.TASK;
const BATCH_SIZE = 10; // number of records per batch
function execute(context) {
// Create search: Not Started tasks, started more than 4 years ago
var ids = [];
var taskSearch = search.create({
type: RECORD_TYPE,
filters: [
['status', search.Operator.ANYOF, ['NOTSTART']], // NOT STARTED tasks
'AND',
['startdate', search.Operator.ONORBEFORE, 'daysago1460'] // On or before 1460 days ago
],
columns: ['internalid']
});
// Accumulate all IDs to delete
taskSearch.run().each(function(result) {
ids.push(result.getValue('internalid'));
return true;
});
// Delete each task in batches synchronously
for (var i = 0; i < ids.length; i += BATCH_SIZE) {
var batch = ids.slice(i, i + BATCH_SIZE);
batch.forEach(function(id) {
try {
record.delete({
type: RECORD_TYPE,
id: id
});
log.audit('Task Deleted', 'Deleted task ID: ' + id);
} catch (e) {
log.error('Delete Failed', 'Unable to delete task ID: ' + id + ' (' + e.message + ')');
}
});
log.audit('Batch Complete', 'Processed batch of ' + batch.length + ' tasks.');
}
log.audit('Done', 'Scheduled script deletion complete for ' + ids.length + ' tasks.');
}
return { execute: execute };
});
Reminders for Safe Deployment:
- Sandbox First: Always test deletion scripts in a sandbox before production use.
- Logs: Review the Script Execution Log for deleted and failed task IDs for accountability and troubleshooting.
- Backups: Consider exporting a CSV of matching tasks before deletion for audit.
The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
To know more about deploying SuiteScript, check these New to NetSuite Articles.
Do you have another way on how to automate your transactions using SuiteScript? Feel free to share them here!