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.
Related videos on Youtube
Author by
Rahul Tapali
Updated on July 09, 2022Comments
-
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 about 12 yearsCan you please elaborate Delayed::Job.find(4).destroy
-
lulalala almost 12 years@clickit it destroy the delayed job that has the id 4.
-
Ian Vaughan over 10 yearsIs there a difference between
destroy
anddelete
? -
JZDBA almost 10 years
destroy
will handle any callbacks that Rails defines - notably, it will clean up any associations where you havedependent: destroy
ordepdendent: nullify
set.delete
just deletes the record from the database. In general, unless you have a good reason to, always usedestroy
overdelete
. -
volx757 over 8 yearsThis doesn't do what OP asked. He is trying to kill jobs that are running, not clear the queue.
-
volx757 over 8 yearsAlso, 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 about 5 yearsI 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 about 5 yearsthere 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 doinvoke_job
to run job but it will not delete it -
Tasos Anesiadis over 4 yearsThe 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.