How to remove all hangfire recurring jobs on startup?

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


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);
    }
}

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");