Cancel or Delete Scheduled Job - HangFire

Vijjendra picture Vijjendra · Aug 8, 2018 · Viewed 8.3k times · Source

I have scheduled a Job via using Hangfire library. My scheduled Code like below.

BackgroundJob.Schedule(() => MyRepository.SomeMethod(2),TimeSpan.FromDays(7));

public static bool DownGradeUserPlan(int userId)
    {
        //Write logic here
    }

Now I want to Delete this Scheduled Job later on some event.

Answer

Alex Riabov picture Alex Riabov · Aug 9, 2018

BackgroundJob.Schedule returns you an id of that job, you can use it to delete this job:

var jobId = BackgroundJob.Schedule(() =>  MyRepository.SomeMethod(2),TimeSpan.FromDays(7));

BackgroundJob.Delete(jobId);