-1

I have a custom object and I have created a lot of records in past days, now I want to delete the records which have been created before 7 days. How can I do it?

4
  • 1
    That doesn't sound like the kind of thing that needs to be scheduled.
    – martin
    Commented Jan 17, 2017 at 11:51
  • 3
    Hi Aarv, welcome to SFSE. Please take a moment to visit the help center and read How to Ask. What have you tried? Where are you stuck? Questions which do not demonstrate any research effort or attempt to solve your own problem tend to get closed. If you're not sure where to even start, head on over to Trailhead instead.
    – Adrian Larson
    Commented Jan 17, 2017 at 11:51
  • 4
    Also, you can just use Data Loader for this task. It's a much more appropriate tool for the job, especially if you don't even know how to start writing your own batch.
    – Adrian Larson
    Commented Jan 17, 2017 at 11:52
  • +1 for dataloader, or 2 lines of Anonymous Apex - unless you can provide any further support/desire for scheduled/batch methodology? Commented Jan 17, 2017 at 11:54

2 Answers 2

5

You can just run an anonymous script if "a lot of records" is less than 10k.

delete [SELECT Id FROM MyObject__c WHERE CreatedDate < LAST_N_DAYS:7];

If you have more records than that, I recommend plugging the above SOQL into Data Loader. Export all matching records, then upload the resulting CSV in a delete job.


If you're dead set on writing a batch, you can create a QueryLocator with that query.

public Database.QueryLocator start(Database.BatchableContext context)
{
    return Database.getQueryLocator([/*above query*/]);
}

And your execute method should be pretty straightforward...

public void execute(Database.BatchableContext context, List<SObject> records)
{
    delete records;
}
4

Not sure if this applies, but if it should be scheduled in the sense of: Everyday, delete all records that are more than 7 days old, you can write the batch with the start and execute methods specified by Adrian Larson. In addition, have the batch class implement Schedulable and add a second execute method, like so

global class OldDataDeleter implements Database.Batchable<sObject>,Schedulable{
    static Date today7 = Date.today().addDays(-7);

    public Database.QueryLocator start(Database.BatchableContext context)
    {
        return Database.getQueryLocator('Select ID from Custom_Object__c where CreatedDate < :today7');
    }

    public void execute(Database.BatchableContext context, List<SObject> records)
    {
        delete records;
    }

    global void execute(SchedulableContext ctx) 
    {
        Database.executeBatch(new OldDataDeleter());
    }
}

You can then schedule this batch to run daily within Salesforce setup - there's a button in the Apex Classes section to do so.

5
  • 1
    N.B. global attribute is not required; public is fine
    – cropredy
    Commented Jan 17, 2017 at 20:56
  • 1
    Also note that you can just do Database.executeBatch(this);.
    – Adrian Larson
    Commented Jan 17, 2017 at 21:00
  • good point on both counts
    – Jochen
    Commented Jan 17, 2017 at 21:13
  • Won't this fail if you are over 10K records? Commented Feb 5, 2019 at 22:47
  • 1
    No, this deletes the default size of 200 records at a time, for each call to execute(Database.BatchableContext context, List<SObject> records), which is called as often as necessary. If you have a lot of stuff going on in trggers and flows of the object, you can even lower that number by passing a second parameter to the executeBatch call.
    – Jochen
    Commented Feb 6, 2019 at 23:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .