+ Start a Discussion

Is there a way to mass delete all Scheduled Jobs? After Sandbox refresh

There is an "idea" to disable jobs, but is there a way to mass delete them all using Apex?   Would like to delete the following:


Dashboard Refresh

Data Export

Scheduled Apex

Report Run





Kent ManningKent Manning

IF there is a way to do this, I also would like to know how.  I have a number of secheduled jobs that have to be deleted everytime we refresh the sandbox.  There has got to be an easier or quicker way to do this.





You can use Dataloader to delete them.  The object is called scheduled jobs but you have to click the checkbox to show All. 

Kent ManningKent Manning
Thanks for the reply
Andrew WheelerAndrew Wheeler
It seems that you can only use Dataloader to 'export' the ScheduledJobs(CronTrigger) records out. When you go to try and 'delete' them again using Dataloader, ScheduledJobs(CronTrigger) is not in the full list of selecteable objects. Bit of a pain...
As commented above you can export all scheduled jobs but can't delete that way.  This will at least give you the number of times you have to run a script like this one:

List<CronTrigger> cronstodelete = [Select Id from CronTrigger Limit 100];
for(CronTrigger CT: cronstodelete){
I was there figuring out how to delete those bunch of jobs then got a solution somewhere

Credits to someone
Global class massDeleteScheduledJobs implements Database.Batchable<sObject> {
// query all the scheduled jobs from the system
String query = 'select Id from CronTrigger';
global Database.QueryLocator start(Database.BatchableContext BC){
    /** Return all the scheduled jobs for processing  **/
    return Database.getQueryLocator(query);
global void execute(Database.BatchableContext BC, List<CronTrigger> scope){
    If (scope.size() > 0){
    For (Integer i = 0; i < scope.size(); i++){ 
global void finish(Database.BatchableContext BC){

Marcelo CostaMarcelo Costa
Just as a follow up question... Why to create a batchable if the limit of scheduled jobs is 100??
It will go over 100 if you count dashboard refreshes, scheduled reports, etc.   

What I ended up doing was running the System.abortJob() method in a @future method that gets spinned up by the SandboxPostCopy-interface class that runs automatically after sandbox refresh/create.   That class is allowed to spin up up to 50 @future methods, each of which can contain 150 DMLs (System.abortJob() methods).   
Ken ShvetzKen Shvetz
Hi, for those of us who are admins (not developers), could someone please provide the steps for creating the above (Apex trigger/batch class) in dev console?
In developer execute anonymous window:
List<CronTrigger> lstCTs = [ SELECT Id FROM CronTrigger LIMIT 150 ];
System.debug( 'Size:' + lstCTs.size() );
For (CronTrigger ct : lstCTs) System.abortJob(ct.Id);

Execute as many times as needed until they are all aborted.