[SalesForce] Mass deletion of records through apex

Aim- I want to delete any record within custom object training__c where the created date is greater than 6 months from today's date.

I would like to run this job daily.

I apologise if the following below has errors I just wrote this on my iPhone, Does the following logic below make sense ?

Looking forward to your help

List listsObject = [Select Id from training where Createddate <= ( Current Date – 6 months) Limit 9000];Delete

Best Answer

You could create a class that implements the schedulable interface and do something like this:

global class ScheduledDelete Implements Schedulable {

    global void execute(SchedulableContext sc) {

        massDelete();

    }

    public void massDelete() {
        List <training__c> listtoDelete = [Select Id FROM training__c WHERE CreatedDate CreatedDate = LAST_N_DAYS:180];
        delete listtoDelete;
    }


}

Then, you can schedule the class to run daily from an anonymous block or by going to Setup > Develop > Apex Classes > Schedule Apex.

Related Topic