function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kirill_YunussovKirill_Yunussov 

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

 

 

 

https://sites.secure.force.com/success/ideaView?id=08730000000HBnU

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.

 

kent

 

MissaDevMissaDev

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...
sobroachsobroach
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){
    System.abortjob(CT.Id);        
}
NewBie09NewBie09
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++){ 
            System.abortJob(scope[i].Id); 
            }
        }
}
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??
Kirill_YunussovKirill_Yunussov
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?
narsavagepnarsavagep
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.