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?
-
1That doesn't sound like the kind of thing that needs to be scheduled.– martinCommented Jan 17, 2017 at 11:51
-
3Hi 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
-
4Also, 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?– Simon LawrenceCommented Jan 17, 2017 at 11:54
2 Answers
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;
}
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.
-
1
-
1Also note that you can just do
Database.executeBatch(this);
.– Adrian Larson ♦Commented Jan 17, 2017 at 21:00 -
-
-
1No, 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.– JochenCommented Feb 6, 2019 at 23:48