Delete a job that may be running
This sample demonstrates how to delete a job through the API.
If you attempt to delete a job that has running instances, you will get an exception. Before deleting a job you should terminate all running instances, as shown below.
public void DeleteJob(Job job)
{
// if you want to make sure the job doesn't get executed while you're waiting for executing instances to complete, put it on hold
job.UpdateHoldType(HoldType.DisableAll, MissedJobCheckOptions.NoCheck);
// get the latest status for the job and check to see if there are running instances
var status = job.GetStatus(true);
if (status.ActiveInstances > 0)
{
// if there are running instances, prompt for confirmation before terminating if appropriate
// if you want to continue deleting, terminate all active instances, and then wait until they have finished
job.Terminate(TerminationOptions.TerminateAllInstances, 0);
// refresh the status from the server once per second until all running jobs have been terminated
while (status.ActiveInstances > 0)
{
System.Threading.Thread.Sleep(timespan.FromSeconds(1));
status = job.GetStatus(true);
}
}
// then you can delete the job
job.Delete();
}