Over time, NetSuite environments can accumulate a large number of old, cancelled Sales Orders. While retaining recent transactions may be required for compliance, many organizations choose to periodically purge records that are both cancelled and very old (e.g., 5+ years) to streamline reporting, reduce storage, and improve performance.
Using SuiteScript, this process can be automated—freeing admins from tedious manual review and deletion.
Example Script
The script below demonstrates how to delete Sales Orders with:
- Status: Cancelled (this is status code "SalesOrd:C")
- Created Date: On or before 5 years ago (calculated via "daysago1825").
/**
* @NApiVersion 2 .x
* @NScriptType ScheduledScript
*/
define(['N/search', 'N/record', 'N/log'], function(search, record, log) {
function execute(context) {
// Search for Sales Orders in 'Cancelled' status older than 5 years, for possible deletion
var salesOrderSearch = search.create({
type: search.Type.SALES_ORDER,
filters: [
["status", search.Operator.IS, "SalesOrd:C"], // Status = Cancelled
"AND",
["datecreated", "onorbefore", "daysago1825"] // Created on or before 5 years ago
],
columns: ["internalid", "tranid", "datecreated"]
});
var deleteCount = 0;
var errorCount = 0;
salesOrderSearch.run().each(function(result) {
var soId = result.getValue('internalid');
var tranId = result.getValue('tranid');
var created = result.getValue('datecreated');
try {
// Attempt to delete the Sales Order
var deletedId = record.delete({
type: record.Type.SALES_ORDER,
id: soId
});
log.audit('Deleted Sales Order', 'Internal ID: ' + deletedId + ' | Tran ID: ' + tranId + ' | Created: ' + created);
deleteCount++;
} catch (e) {
log.error('Failed to Delete Sales Order', 'Tran ID: ' + tranId + ', Reason: ' + e.message);
errorCount++;
}
// Process up to 100 records per run to avoid governance issues
return deleteCount < 100;
});
// Summary
log.audit('Sales Order Deletion Complete', 'Total deleted: ' + deleteCount + ', Errors: ' + errorCount);
}
return { execute: execute };
});
Best Practice Checklist
- Test in Sandbox: Always validate scripts in a sandbox environment first.
- Logging: Use log.audit and log.error for a clear record of what was deleted and any issues.
- Permissions: Script must run under a role with permission to delete Sales Orders.
- Record Dependencies: Sales Orders linked to other transactions (e.g., invoices, fulfillments) may not be eligible for deletion.
- Compliance: Check with your organization’s data retention and compliance policies before deleting transaction records.
- Error Handling: This example catches and logs errors to ensure visibility if a record cannot be deleted.
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!