Search code examples
c#asp.netjob-schedulinghangfire

Cancel or Delete Scheduled Job - HangFire


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.


Solution

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