Deleting all pending tasks in Celery (purge the queue)
Codemia Knowledge Hub ·
The fastest way to delete all pending Celery tasks is app.control.purge() (or celery -A myapp purge from a shell), which removes the waiting messages from the broker queue. For finer control, revoke specific tasks by ID or purge individual named queues. The methods differ in how they treat tasks that workers have already fetched or started, so pick based on whether you need a clean slate or selective cancellation.
Where Celery tasks live
| State | Location | Can Be Deleted? |
| Pending (queued) | Broker (RabbitMQ, Redis, etc.) | Yes, via purge |
| Reserved (prefetched by worker) | Worker memory | Yes, via revoke |
| Active (currently executing) | Worker process | Yes, via revoke with terminate=True |
| Completed / Failed | Result backend | No (already finished) |
Purging only removes messages from the broker queue. Tasks a worker has already fetched (reserved) or started (active) need revocation.
Purging from Python
The most common approach, removing pending messages from the default queue:
To target a specific queue:
With RabbitMQ, you can also purge through the management tooling. This is the classic (v1, Python) rabbitmqadmin syntax:
The v2 rabbitmqadmin (rabbitmqadmin-ng) uses subcommands and flags instead: rabbitmqadmin queues purge --name celery and rabbitmqadmin queues list.
Purging with the celery CLI
With shell access, celery purge is the simplest option:
To see what is queued before purging:
Revoking specific tasks
To cancel particular tasks rather than everything, call revoke with the task ID:
Several at once:
From the AsyncResult returned when you dispatch a task:
terminate=True sends SIGTERM by default. If the task ignores it (common with subprocess calls or C extensions), escalate to signal='SIGKILL', which prevents cleanup code from running.
Revoking every instance of one task name
Combine inspection with revocation:
Clearing the broker directly
When Celery commands are unavailable (workers down, or the app cannot connect), work on the broker itself.
For Redis:
With the Redis transport, a task sent with a non-zero priority lands in a separate list such as celery\x06\x166 (the queue name, a \x06\x16 separator and the priority step), so DEL celery alone leaves those behind. celery purge clears them too.
FLUSHDB deletes everything in that Redis database. If it also holds cache, sessions or rate limits, all of it is gone, so target the queue keys instead.
For RabbitMQ:
Preventing task buildup
Task expiration
Set an expiration so stale tasks are discarded automatically:
Do not confuse expires with countdown. countdown=120 delays execution by 120 seconds, while expires=120 discards the task if it has not started within 120 seconds.
Rate limiting
Stop producers from overwhelming the queue:
Deduplication
Stop two copies of a task from running at the same time with a lock. This does not stop duplicates from being queued; the second copy is still delivered and simply exits early:
Monitoring queue depth
Set alerts on queue length (for example with Prometheus and Flower). Finding thousands of stuck tasks after the fact is much harder than preventing the buildup.
Comparing the methods
| Method | Scope | Requires Workers | Safe for Production |
app.control.purge() | All queued tasks | No | Use with caution |
celery purge CLI | All or specific queues | No | Use with caution |
app.control.revoke(id) | Single task | Yes (for active tasks) | Yes |
| Broker flush (Redis/RabbitMQ) | All data in broker | No | Last resort only |
| Task expiration | Stale tasks | No | Yes (preventive) |
Why tasks keep running after a purge
- Active tasks are untouched.
app.control.purge()only removes broker messages. Tasks workers are executing keep running; userevoke(terminate=True)for them. - Prefetched tasks are untouched. Workers prefetch tasks into memory, and purging the queue does not affect them. Restarting the workers after purging does not help either: on shutdown a worker returns its unacknowledged prefetched messages to the queue (it logs
Restoring 4 unacknowledged message(s)), so they come back. Revoke them individually, or stop the workers first, then purge, then start the workers again:
Inspect the queue state with celery inspect before and after purging to confirm the result.
Related reading
- Deleting queues in RabbitMQ
- Demultiplexing messages from a queue to process in parallel streams using amqp?
- Deserialize Avro messages into specific datum using KafkaAvroDecoder
- Deserializing a kafka message without schema registry
- Deleting DataFrame row in Pandas based on column value
- Deleting folders in python recursively
- ''Dense'' object has no attribute ''op''
- Dependency 'org.springframework.bootspring-boot-starter-web2.3.0.RELEASE' not found
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.