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
RupaliJRupaliJ 

How to delete existing scheduled job using apex?

Hi,

 

I am inserting rates on daily basis. For this, I have scheduled apex class using following code:

 

scheduledGenerateRates m = new scheduledGenerateRates(); 
cronId = system.schedule('Update Rate Schedule', '0 0 * * 1-12 ? *', m);

 Afterthat, if some error occured while inserting records, I want to delete existing scheduled job. ISo, i have used abortjob function to do this:

 

List<CronTrigger> cron = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :scheduledJobId];
for(CronTrigger CT:cron)
{ 
   try
   {
       System.abortjob(CT.id); 
   }catch(Exception e1)
   {
       System.debug('--- Unable to delete scheduled job with Scheduled_Geocoder_Job_Id = ' + CT.id + ' because ' + e1.getMessage());
    }
}

 And in the debug log, i am getting:

Aggregations:0|SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :scheduledJobId
07:00:03.778 (2778266000)|SOQL_EXECUTE_END|[170]|Rows:1
07:00:03.778 (2778504000)|SYSTEM_METHOD_ENTRY|[175]|System.abortJob(String)

 But, still in the list of Setup - > Monitoring ->Scheduled Jobs, I am able to see that job.

 

Please suggest me some solution to delete scheduled job from here.

 

Thanks.

RupaliJRupaliJ

I have gone through this link. But when I am aborting job, through my code, I can see in the debug log that job is aborted. Though job is aborted, I am able to see it in the list of Scheduled Job.

 

Why this is happening?

Thanks.

dmchengdmcheng

I think the jobs remain in AsyncApexJob regardless of status.  You cannot delete them but I think the system will remove records after a certain time period.

RupaliJRupaliJ

I have waited for 15-20 minutes after aborting job. But, still its not deleted from scheduled job list. And one more thing, when I a am trying to schedule same job again, its giving me error that job is already scheduled.

 

Thanks.

Here-n-nowHere-n-now

The aborted jobs don't seem to stay in the "montiored" list anymore.  Once I ran the script to abort them, they instantly drop from the list.

MJ09MJ09

I'm seeing the Scheduled job stay in the crontrigger list after I've done a System.abortJob().  

 

My code is running in the Schedulable class's execute() method:

 

global void execute(SchedulableContext context) {

    // Do some stuff

 

    System.abortJob(context.getTriggerId());

}

 

This should abort the job and remove it from the schedule. At least that's what it's done in the several other orgs where I've written similar code.

 

(My org is on na9, if that matters.)  Is this a recurrence of a bug?

SForceBeWithYouSForceBeWithYou

It looks like you want to abort/delete these jobs because you are running into unique name conflicts for the Scheduled Job.

 

Just append a timestamp to the end of your job name and you won't run into this conflict.