RQ - Empty & Delete Queues
Asked Answered
C

8

19

I'm using RQ, and I have a failed queue with thousands of items, and another test queue I created a while back for testing which is now empty and unused. I'm wondering how to remove all jobs from the failed queue, and delete the test queue altogether?

Apologies for the basic question, but I can't find info on this in the RQ docs, and I'm completely new to both Redis and RQ... Thanks in advance!

Casement answered 23/7, 2014 at 15:48 Comment(0)
G
48

Cleanup using rq

RQ offers methods to make any queue empty:

>>> from redis import Redis
>>> from rq import Queue
>>> qfail = Queue("failed", connection=Redis())
>>> qfail.count
8
>>> qfail.empty()
8L
>>> qfail.count
0

You can do the same for test queue, if you have it still present.

Cleanup using rq-dashboard

Install rq-dashboard:

$ pip install rq-dashboard

Start it:

$ rq-dashboard
RQ Dashboard, version 0.3.4
 * Running on http://0.0.0.0:9181/

Open in browser.

Select the queue

Click the red button "Empty"

And you are done.

Python function Purge jobs

If you run too old Redis, which fails on command used by RQ, you still might sucess with deleting jobs by python code:

The code takes a name of a queue, where are job ids.

Usilg LPOP we ask for job ids by one.

Adding prefix (by default "rq:job:") to job id we have a key, where is job stored.

Using DEL on each key we purge our database job by job.

>>> import redis
>>> r = redis.StrictRedis()
>>> qname = "rq:queue:failed"
>>> def purgeq(r, qname):
...   while True:
...     jid = r.lpop(qname)
...     if jid is None:
...         break
...     r.delete("rq:job:" + jid)
...     print(jid)
...
>>> purge(r, qname)
a0be3624-86c1-4dc4-bb2e-2043d2734b7b
3796c312-9b02-4a77-be89-249aa7325c25
ca65f2b8-044c-41b5-b5ac-cefd56699758
896f70a7-9a35-4f6b-b122-a08513022bc5
Gubernatorial answered 23/7, 2014 at 16:39 Comment(7)
Thanks very much for your detailed reply. From your answer, I tried using redis-cli with del and using Redis from Python. Unfortunately listing keys in either methods appears to be listing jobs, not the queues, attempting to delete the queues through these methods doesn't work (I get 0 returned each time)?Casement
@Casement I rewrote the answer to use rq library and optionally rq-dashboardGubernatorial
Thanks Jan, I really appreciate your input. Using rq I get the error redis.exceptions.ResponseError: unknown command 'EVALSHA' when I call qfail.empty()?Casement
@Casement What version of Redis do you have? EVALSHA is present since 2.6.0, I have version 2.8.6.Gubernatorial
I'm on version 2.2 - is there a way to delete jobs and queues compatible with v2.2?Casement
@Clan I put back into my question the purging function purgeq, which shall work. Try it.Gubernatorial
Brilliant, thanks for that Jan. I had just deleted the failed queue, but that solution there is nice and concise. Cheers for the help.Casement
A
13

- 2016 -

You can now use rq's empty option form command line:

/path/to/rq empty queue_name

So you can use it to empty any queue not just the failed one

Ahvenanmaa answered 30/11, 2016 at 13:25 Comment(3)
I tried $ rq empty default --config myproject.config Error: no such option: --configAbukir
What happened if you don't use the config flag? What RQ version do you have?Ahvenanmaa
without config flag everything works as expected. Version 0.7.1Abukir
P
4

none of the above solutions worked failed Queue is not registered under queues

so I move all of the failed jobs to default Queue and use

rq empty queue_name --url [redis-url]

Pewee answered 21/1, 2020 at 9:18 Comment(0)
P
2

Here's how to clear the failed job registry using django_rq:

import django_rq
from rq.registry import FailedJobRegistry

queue = django_rq.get_queue("your_queue_with_failed_jobs")
registry = FailedJobRegistry(queue=queue)

for job_id in registry.get_job_ids():
  registry.remove(job_id)
Perfume answered 18/11, 2020 at 16:53 Comment(1)
Worked well for me in March 2023Telex
S
1

Monitoring tool rqinfo can empty failed queue.
Just make sure you have an active virtualenv with rq installed, and run

$ rqinfo --empty-failed-queue

See rqinfo --help for more details.

Stepfather answered 10/3, 2015 at 23:4 Comment(1)
This option no longer available. I'm on RQ 0.5.5.Nichani
I
1

you can just login to redis and clear all queues

to login

user@user:~$ redis-cli

enter this command and hit enter

FLUSHALL

And you're done

Edit: This will delete everything stored in redis

Incontinent answered 10/1, 2019 at 6:29 Comment(2)
this would, of course, delete EVERYTHING from redis, and not just the RQ queues. so not a great answer.Buckles
@Buckles Yes, you are absolutely right. I must have mentioned that, I will add that informationIncontinent
S
1

- 2022 -

I was struggling with this as well and this is a piece of code which works for me.

It loops over queues name (in my case, 'default' and 'low'), fetch all failed jobs for each queue and remove them

import django_rq
from rq.registry import FailedJobRegistry
from redis import Redis
from rq.job import Job
from django.conf import settings
redis = Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT)
queues = ["default", "low"]
for q in queues:
    queue = django_rq.get_queue(q)
    registry = FailedJobRegistry(queue=queue)
    for job_id in registry.get_job_ids():
        job = Job.fetch(job_id, connection=redis)
        registry.remove(job)
Sirup answered 14/4, 2022 at 7:57 Comment(0)
H
-1

By default 'rq' jobs are prefixed by 'rq:job'. So you can delete these jobs from the redis using following command,

redis-cli KEYS rq:job:* | xargs redis-cli DEL
Hecklau answered 17/12, 2019 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.