Let’s delete some records in batches !!

Ramyajit Choudhury
4 min readAug 13, 2022

So, I am still relatively new to Salesforce, and every day, I find myself, in a fresh pickle, so recently I have been trying to figure out the asynchronous apex, and thought to myself about implementing a batch apex, to delete week-old records.

So while surfing the internet, I found a few articles, on how to implement a batch class, but I wanted to go the extra mile and wanted to automate the process, i.e. the batch will run every day and keep on deleting the record! No trust me this is not the ramblings of a mad man, Salesforce does provide you the feature to automate the thing *winks*, I had some trouble scheduling the task, so here is an article on how to schedule batch classes using a scheduler, hope this will help someone in their Salesforce Development Journey.

Without further ado, let’s get to the murky coding grounds!

Step 1: Creating the Batch Class

Let’s take a Scenario where you have a custom object Outgoing_Order__c, and you want to delete the records, which are a week old. Now, we will be creating a batch class to implement the logic of deleting the records. Let’s name the class OutgoingOrdersRecordsDeletion. Added bonus: I’ll be implementing an exception-handling logic in the finish function too so that if the batch does not have any records to delete, you get a custom message which helps you understand what went wrong!!

Batch Class to Delete Week Old Records

Now that, we are done with the Batch Class half of the battle is won, onto the next part of writing the Scheduler, this is the easiest part, now for the Scheduler, there are two approaches: either you can write a CRON Expression which will make a timely call to the batch class daily, or you can write a Scheduler Class, where an instance of the Batch Class is being Called and we will schedule the scheduler to run on a daily basis!

I will be performing the latter approach, in that way, I will be able to display more functionality in the Salesforce Platform! (You’re welcome!!!)

Step 2: Creating the Scheduler Class

Let’s create a Scheduler class named OutgoingOrdersDeletionScheduler, where we will create an instance of the Batch class and execute that instance in batches sizes of 200 [Note 200 is the maximum size for DML operations]. For more information please refer to Governor Limits of Salesforce.

Scheduler Class Calling the Batch Class

Step 3: Scheduling the Scheduler Class to be executed every day

Type Apex Classes in the Quick Find Box of the Setup Page of your Org, you will be redirected to the list of apex classes in your Org, there you will see the Option to Schedule Apex on the top of the List

Schedule Apex Option

Click on the Schedule Apex Option, you will be redirected Schedule Apex Setup Page, where you’ll have to fill out the following details :

  1. Job Name: Here you can give a Name of your choice to the deletion Job
  2. Apex Class: Here you have to select the Class which you want to be executed on daily basis, in our case (The Scheduler class: OutgoingOrdersDeletionScheduler)
  3. Frequency: Here you have to select Weekly or Monthly (Since we want the class to run daily we will select Weekly)
  4. Start: Here you have to provide a date from when you want the Job to start
  5. End: Here you have to provide a date when you want the Job to end
  6. Preferred Start Time: Here you have to provide the time when you want the job to be executed (Eg: 10:00 PM)

After filling out all the details, your Schedule Apex should look something similar to this:

Schedule Apex Setup

And viola, you can pat yourself on the back, you did it, you just automated a process to delete week-old records from your custom object in Salesforce!

Hope this article helps you if you stumble across this!! If you have any feedback or suggestion, you can reach out to me through my email at ramyajit0311@gmail.com.

--

--