Cancel a running job

May I know how to cancel a enqueued running job (CancellationToken) ?
I read some post mentioned about Background.Cancel(jobId) but couldn’t find it in code.
I found that is BackgroundJob.Delete(), but does it delete scheduled job or it will cancel the running job?
Pls advice. tq

BackgroundJob.Delete method will do both – remove a job and cancel it (if you are using cancellation tokens).

Thank you for reply.
Do you mind to share, how Hangfire request job to cancel via cancellationToken?
In my program, I have token.ThrowIfCancellationRequested() in loop.
I just would like to understand, how the logic work. Becoz I might need to have subclass it for standardization purpose.


Beside, is it possible to KILL the running job?
May be becoz of certain reason, the job just “hang”? I wan till that thread. Does hangfire support it?
Anyway to enhance it?

How could I cancel a scheduled job which is running but still retain it scheduling setting?
I tried to use BackgroundJob.Cancel() but it return exception

SqlException: Conversion Failed when converting nvarchar value “recurringJobId” to data type int

following this topic!

Hi guys,

Isn’t there any example about how to achieve this?

I’ve landed here with more or less the same question. We enqueue long-running jobs in Hangfire. There is no “Cancel” button for jobs in the built-in dashboard, only a “Delete” button.

Using that Delete button will remove the Job from the running jobs in the built-in dashboard (near) instantly. And once the running job checks for cancellation to exit it’ll actually exit with an OperationCancelledException.

However, consider the case where a job either (a) fails to nicely check for cancellation or (b) somehow got stuck because of a coding error: now the job keeps running, even though the built-in dashboard suggests it is already “Deleted”.

I’m pretty new to working with Hangfire in detail, but I had expected instead of a “Deleted” button, to get these:

  • Request Cancellation”: this would cancel the job as soon as it checks for cancellation, and only once it actually does so /exits will it move to the “Deleted” (or similar) list.
  • Force Stop”: this would be the kill-switch, aborting the process ungracefully, but again only moving it to the “Deleted” (or similar) list once it actually was killed.

I’m unsure if posting here is wise. I considered opening an issue on Github as well as starting a new thread, but my thoughts seemed enough related to the OP that I ended up posting here. Suggestions to repost my ramblings elsewhere are also welcome.

1 Like

Does the following documentation topic: Using cancellation tokens provide enough info?

the use case is this : I have a long running job recurring job. At some point while it is processing I want to be able to cancel this job. I implemented JobCancellationToken per the docs. I register the job with :

var token = new JobCancellationToken(true);
RecurringJob.AddOrUpdate(“MyJobId”, o => o.Run(token), Cron.Minutely,null, myQueName");

The docs state that BackgroundJob.Delete(id) will fire the token. The problem is that you cannot pass “MyJobId” into the Delete method. You instead need to pass the integer which is the Id of the actual Job table in the database. HOW do I know which job instance to delete. I do not see any tie between the registered jobid of “MyJobId” and the processing jobId of say 3?

Thanks in advance!


so the somewhat smelly workaround I found was as follows

var mon = JobStorage.Current.GetMonitoringApi();
var processingJobs = mon.ProcessingJobs(0, int.MaxValue);
var jobInstanceIdsToDelete = new List();
processingJobs.Where(o => o.Value.Job.Method.Name == “TheMethodNameThatIRegistered”).ToList().ForEach(x => jobInstanceIdsToDelete.Add(x.Key));

foreach (var id in jobInstanceIdsToDelete)

You may also want to Remove the job so that it is not ‘requed’ by hangfire

So while this works its certainly nothing that I would be proud of. It seems that there should be a way to cancel all instances of a registered job via the jobid that it was registered with.

If anyone has more smarts than me please let me know!

1 Like