Cian Cian - 1 year ago 120
Python Question

RQ - Empty & Delete Queues

I'm using RQ, and I have a

queue with thousands of items, and another
queue I created a while back for testing which is now empty and unused. I'm wondering how to remove all jobs from the
queue, and delete the
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!

Answer Source

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
>>> qfail.empty()
>>> qfail.count

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

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download