How to remove all hangfire recurring jobs on startup?
Asked Answered
F

3

47

I am looking at using Hangfire as a job scheduler for recurring jobs. So configuring them is simple with AddOrUpdate, but then how do i delete it? I don't want to pollute my code with RecurringJob.RemoveIfExists() when that job has been deleted and then have to remember to delete it later.

Is there a way to get a list of all recurring jobs and delete them when the server starts and so my code will re add them in every time? If not, if there a better way?

e.g.

Application version 1: Added new Hangfire recurring job Do something 1

Application version 2: Added new Hangfire recurring jobs Do something 2 and Do Something 3

Application version 3: Removed Hangfire recurring job Do something 2

Problem: the job will still exist on the server with error "Could not load type..." and needs to be deleted.

Franzen answered 27/10, 2016 at 6:13 Comment(0)
S
90

A bit late on this one but hopefully it will help someone else. I got stuck in the same situation. In the end the answer on HangFire recurring task data helped me out.

I use the JobStorage to loop through all recurring jobs and remove each in turn as below:

using (var connection = JobStorage.Current.GetConnection())
{
    foreach (var recurringJob in connection.GetRecurringJobs())
    {
        RecurringJob.RemoveIfExists(recurringJob.Id);
    }
}

I am sure there is a nicer way out there but I couldn't find it

Sternutatory answered 12/12, 2016 at 11:53 Comment(2)
This answer is outdated.Ceroplastics
@ChrisWeber, it works with 1.7.18 which is latest at this time. I only pasted the code, no changes made.Armanda
A
24

paul's answer was helpful but API api seems to have changed. Using Hangfire 1.6.20 I needed to get the recurring jobs from StorageConnectionExtensions

using (var connection = JobStorage.Current.GetConnection()) 
{
    foreach (var recurringJob in StorageConnectionExtensions.GetRecurringJobs(connection)) 
    {
        RecurringJob.RemoveIfExists(recurringJob.Id);
    }
}
Alsace answered 31/10, 2018 at 1:41 Comment(3)
Need to add a using Hangfire.Storage at the top of the file, but yeah, this is what I needed to drop all recurring jobs. Thanks!Bristletail
As the StorageConnectionExtensions class is self explanatory, the GetRecurringJobs() method is an extension method of the Connection object, so no need to call it via the static extension class, use connection.GetRecurringJobs() instead.Delirium
Seems it will delete also the history of All recurring Jobs execution. So this approach should not be used for Production.Ligation
E
3

according to hangfire docs

public static void RemoveIfExists(
    string recurringJobId
)

with this method, you can pass the job id which you want to delete.

RecurringJob.RemoveIfExists("exampleClassName.exampleFunctionName");
Elanorelapid answered 16/8, 2021 at 8:1 Comment(1)
I don't want to pollute my code with RecurringJob.RemoveIfExists() when that job has been deleted and then have to remember to delete it later.Unmusical

© 2022 - 2024 — McMap. All rights reserved.