How to delete mass records using Map/reduce script?

suitescript 2.0 map/reduce example
netsuite suitescript 2.0 examples
how to mass delete records in netsuite

I have created a Map/Reduce script which will fetch customer invoices and delete it. If I am creating saved search in UI based on the below criteria, it shows 4 million records. Now, if I run the script, execution stops before completing the "getInputData" stage as maximum storage limit of this stage is 200Mb. So, I want to fetch first 4000 records out of 4 million and execute it and schedule the script for every 15 mins. Here is the code of first stage (getInputData) -

            var count=0;
                var counter=0;
                var result=[];
                var testSearch = search.create({
                    type: 'customrecord1',
                    filters: [ 'custrecord_date_created', 'notonorafter', 'sta​rtO​fLa​stM​ont​h' ],
                    columns: [ 'internalid' ]
                }); 
                do{
                    var resultSearch = testSearch.run().getRange({
                        start : count,
                        end   : count+1000
                    });
                    for(var arr=0;arr<resultSearch.length;arr++){
                        result.push(resultSearch[arr]);
                    }
                    counter = count+counter;                        
                }while(resultSearch.length >= 1000 && counter != 4000);
                return result;

During creating the saved search, it is taking long time, is there any work around where we can filter first 4000 records during saved search creation?

Why not a custom mass update?

It would be a 5-10 line script that grabs the internal id and record type of the current record in the criteria of the mass update then deletes the record.

Map / Reduce Mass Deletion Script, I wrote a map reduce script to mass delete records using either a saved search or CSV as input as this comes up a lot. My script work excellent  I wrote a map reduce script to mass delete records using either a saved search or CSV as input as this comes up a lot. My script work excellent in some accounts that have 20-30 queues and deletes 10k journal entries in a flash.

I believe this is what search.runPaged() and pagedData.fetch() is for.

search.runPaged runs the current search and returns summary information about paginated results - it does not give you the result set or save the search.

pagedData.fetch retrieves the data within the specified page range.

Map/Reduce script in NetSuite with SuiteScript 2.0, that can be used to handle substantial number of records. The searched records are structured into key/value pairs where each key-value pair is read in parallel and passed on to next step for processing. Map / Reduce script designed to delete records in NetSuite safely and effectively. How to Deploy. Add the Map / Reduce and library files to the file cabinet; Create a new Map / Reduce script; Create a script parameter for the script with the id: ‘_tf_saved_search’ so the final name will be ‘custscript_tf_saved_search’

If you are intent on the Map/Reduce you can just return your created search. Netsuite will run it and pass each line to the next phase. You can even use a saved search where you limit the number of lines and then in your summarize phase re-trigger the script if there's anything left to do.

The 4k record syntax though is:

var toDelete = [];
search.run().each(function(r){
    toDelete.push(r.id); 
    return toDelete.length < 4000;
});
return toDelete;

finally I normally do this as scheduled mass update. It will tend to interfere less with any production scheduled and map/reduce scripts.

/**
 * @NApiVersion 2.x
 * @NScriptType MassUpdateScript
 */
define(["N/log", "N/record"], function (log, record) {
    function each(params) {
        try {
            record.delete({
                type: params.type,
                id: params.id
            });
            log.audit({ title: 'deleted ' + params.type + ' ' + params.id, details: '' });
        }
        catch (e) {
            log.error({ title: 'deleting: ' + params.type + ' ' + params.id, details: (e.message || e.toString()) + (e.getStackTrace ? (' \n \n' + e.getStackTrace().join(' \n')) : '') });
        }
    }
    return {
        each:each
    };
});

Delete Transaction Records from Netsuite, All the work is done in manageable chunks in the map and reduce portion of the script to prevent timeout. Q: Some records did not delete. The Map/Reduce type of script is only available in SuiteScript 2.0. When the Map/Reduce script is executed, parallel processing across multiple queues kicks in unless the script was required to use one queue and therefore this type of script can be used for bulk processing or long running background processes.

NetSuite Mass Delete Tool, The Map/Reduce type of script is only available in SuiteScript 2.0. is executed, parallel processing across multiple queues kicks in unless the script The main task of deleting records is done here and thus no reduce stage  The Map/Reduce script works similarly, but once it is triggered, it breaks the processing into four distinct phases: The getInputData phase is where you gather all of the input data you will need to complete the business process. You can use this phase to perform searches, read records, and package your data into a decipherable data structure.

App Engine Java MapReduce Example: bulk deleting entities , Getting started with Java App Engine Map Reduce to delete bulk query to delete all rows in a table with a particular created timestamp. You seem to be using record.delete correctly, except that the instances of a single custom record type are not sequential. Not every i from 4 to 11614 will all be instances of customrecord_uber_billing_report_data .

netsuite, For really huge search results, you can use dedicated Map/Reduce script. to multiple sub-searches, which could be executed in Map or Reduce stages. To delete an entire table, you can do so from Tables & Columns. To delete a table from Rows and Columns: 1. Navigate to System Definition > Tables & Columns. 2. In the Table Names pane, select a table. A list of the columns on the selected table appears in the Column Names pane. 3. Click Delete all Records button

Comments
  • Yes, that might work. But I was trying to test whether we can do it in Map/reduce.
  • Yes, In Map/reduce that's what i have done, to get 4k records. But issue is first it will create saved search returning 4 million records. That will take lot of time. Once saved search is created it will return first 4k records to next stage. I am looking for filtering the result during search creation, anything in search.filter
  • If you just create and return the search object. Don’t run the search.