Pattern: Job to delete many records

Pattern for efficient (i.e. use transaction for multiple deletes and use doDelete), stable (i.e. don’t make one huge transaction but commit and restart transaction periodically) and verbose (count down the number of remaining records) job to delete many records in a table.

static void deleteJob(Args _args)
{
    #define.TABLE(myTable)         //set
    #define.CONDITION(true)        //set
    #define.TRANSACTIONSIZE(100)   //set
    #TABLE #TABLE;
    int i = 1;
    ;
    while (i>0)
    {
        i=0;
        info(strfmt("%1",(select count(RecId) from #TABLE where #CONDITION).RecId));
        infolog.viewUpdate();
        ttsbegin;
        while select forupdate #TABLE where #CONDITION
        {
            #TABLE .dodelete();
            i++;
            if(i>=#TRANSACTIONSIZE)
                break;
        }
        ttscommit;
    }
}