Destroying all delayed job in rails

27,201

Solution 1

You can invoke rake jobs:clear to delete all jobs in the queue.

Solution 2

In addition to the rake task, DelayedJob jobs are just a normal ActiveRecord model, so if you're in Ruby code you can do what you like with them:

Delayed::Job.destroy_all
Delayed::Job.delete_all
Delayed::Job.find(4).destroy
# etc.
Share:
27,201

Related videos on Youtube

Rahul Tapali
Author by

Rahul Tapali

Updated on July 09, 2022

Comments

  • Rahul Tapali
    Rahul Tapali almost 2 years

    I am using collectiveidea for rails 2.3.8. I am creating array of delayed jobs to perform some tasks, after some time I want to destroy all the delayed jobs which are running. If anyone know the way to do this please help me.

  • Rahul Tapali
    Rahul Tapali about 12 years
    Can you please elaborate Delayed::Job.find(4).destroy
  • lulalala
    lulalala almost 12 years
    @clickit it destroy the delayed job that has the id 4.
  • Ian Vaughan
    Ian Vaughan over 10 years
    Is there a difference between destroy and delete?
  • JZDBA
    JZDBA almost 10 years
    destroy will handle any callbacks that Rails defines - notably, it will clean up any associations where you have dependent: destroy or depdendent: nullify set. delete just deletes the record from the database. In general, unless you have a good reason to, always use destroy over delete.
  • volx757
    volx757 over 8 years
    This doesn't do what OP asked. He is trying to kill jobs that are running, not clear the queue.
  • volx757
    volx757 over 8 years
    Also, Delayed::Job.find(id).locked_by will give you a PID in the string that's displayed, ex: "host:Your-Host.local pid:6899"
  • morhook
    morhook about 5 years
    I had a running job (it was going to hang for two hours), and after running rake jobs:clear the worker stopped that specific job too.
  • localhostdotdev
    localhostdotdev about 5 years
    there is a bunch of interesting methods/attributes, handler for the yaml of the class handling the job and the job params, last_error for a stacktrace of the last error, etc. job.attributes will give you what is stored in the database. also you can do invoke_job to run job but it will not delete it
  • Tasos Anesiadis
    Tasos Anesiadis over 4 years
    The OP wanted to know how to remove the jobs from the queue from the application side - not from the terminal, so this indeed doesn’t answer his/her question. From the upvotes seems people are coming to this question for the latter though.