Scheduled Jobs to delete records in a batch of 5000

Priyanka_77
Tera Contributor

Hi all,

 

I have wrote below code for scheduled job to delete all the records for specific condition.

But i want to delete all the records in a batch of 5000,since there will be excess data.

How can i do that?

 

var tableArray = ["abc", "cc", "ii", "rr"];

gs.info("@PD Line 3");
for (var i = 0; i < tableArray.length; i++) {

    gs.info("@PD Line 5" + tableArray[i]);

    //gs.info("@PD line 6 row count:"+ gr.getRowCount());
    var techCI = new GlideRecord(tableArray[i]);

    techCI.addEncodedQuery("sys_created_on>javascript&colon;gs.beginningOfOneYearAgo()");

    techCI.query();
    gs.info("@PD line 13 row count:" + techCI.getRowCount() + "Table array:" + tableArray[i]);

    gs.info("@PD line 19 " + techCI.getEncodedQuery());
    //gs.info("@PD line 12 row count:" + gr.getRowCount());
    //gr.deleteMultiple();

}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka_77 

try this

var tableArray = ["x_tsigh_ci_handler_technical_ci_queue", "x_tsigh_ci_handler_ci_transfer_protocol", "x_tsigh_ci_handler_ci_transfer_status", "x_tsigh_ci_handler_ci_chunks"];

gs.info("@PD Line 3");
for (var i = 0; i < tableArray.length; i++) {
    gs.info("@PD Line 5 " + tableArray[i]);

    var techCI = new GlideRecord(tableArray[i]);
    techCI.addEncodedQuery("sys_created_on>javascript&colon;gs.beginningOfOneYearAgo()");

    var batchSize = 5000;
    var deletedCount = 0;

    do {
        techCI.setLimit(batchSize);
        techCI.query();

        var count = techCI.getRowCount();
        gs.info("@PD line 13 row count: " + count + " Table array: " + tableArray[i]);

        while (techCI.next()) {
            techCI.deleteRecord();
            deletedCount++;
        }

        gs.info("@PD line 19 " + techCI.getEncodedQuery());
    } while (count == batchSize);

    gs.info("Total records deleted from " + tableArray[i] + ": " + deletedCount);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka_77 

try this

var tableArray = ["x_tsigh_ci_handler_technical_ci_queue", "x_tsigh_ci_handler_ci_transfer_protocol", "x_tsigh_ci_handler_ci_transfer_status", "x_tsigh_ci_handler_ci_chunks"];

gs.info("@PD Line 3");
for (var i = 0; i < tableArray.length; i++) {
    gs.info("@PD Line 5 " + tableArray[i]);

    var techCI = new GlideRecord(tableArray[i]);
    techCI.addEncodedQuery("sys_created_on>javascript&colon;gs.beginningOfOneYearAgo()");

    var batchSize = 5000;
    var deletedCount = 0;

    do {
        techCI.setLimit(batchSize);
        techCI.query();

        var count = techCI.getRowCount();
        gs.info("@PD line 13 row count: " + count + " Table array: " + tableArray[i]);

        while (techCI.next()) {
            techCI.deleteRecord();
            deletedCount++;
        }

        gs.info("@PD line 19 " + techCI.getEncodedQuery());
    } while (count == batchSize);

    gs.info("Total records deleted from " + tableArray[i] + ": " + deletedCount);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I am getting row count 0.

@Priyanka_77 

are you sure your encoded query is working fine?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

yes. There are records. still getrowcounts is 0.