Deleting all pending tasks in celery?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The fastest way to delete all pending Celery tasks is app.control.purge(), which removes every message from the broker queue. For finer control, you can revoke specific tasks by ID, purge individual named queues, or use the celery purge command-line tool. Each approach has different implications for running workers and active tasks, so choosing the right method depends on whether you need a clean slate or selective cancellation.
Celery Task Lifecycle
Before deleting tasks, it helps to understand where they live in the system:
| 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 that a worker has already fetched (reserved) or started executing (active) require revocation.
Method 1: Purge All Queues Programmatically
The most common approach. This removes all pending messages from the default queue:
To target a specific queue:
When using RabbitMQ, you can also purge through the management API:
Method 2: Celery Command-Line Tool
The celery purge command is the simplest option when you have shell access:
To inspect what is in the queue before purging:
Method 3: Revoke Specific Tasks
When you need to cancel specific tasks rather than purging everything, use revoke with the task ID:
To revoke multiple tasks at once:
For AsyncResult objects returned when you dispatch tasks:
Method 4: Revoke by Task Name
To revoke all instances of a specific task type, combine inspection with revocation:
Method 5: Flush the Broker Directly
When Celery commands are unavailable (worker is down, app cannot connect), you can flush the broker directly:
For Redis:
For RabbitMQ:
Preventing Task Buildup
Rather than purging reactively, these patterns prevent excessive task accumulation:
Task Expiration
Set an expiration time so stale tasks are automatically discarded:
Rate Limiting
Prevent task producers from overwhelming the queue:
Task Deduplication
Avoid duplicate tasks using a lock:
Comparison of 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) |
Common Pitfalls
- Purging does not stop active tasks.
app.control.purge()only removes messages from the broker queue. Tasks already being executed by workers continue running. Userevoke(terminate=True)for active tasks. - Worker prefetch hides tasks from purge. Workers prefetch tasks from the broker into memory. Purging the queue does not affect these prefetched tasks. Either revoke them individually or restart the workers after purging.
- Using terminate=True with SIGTERM may not work. If the task ignores SIGTERM (common with subprocess calls or C extensions), escalate to
signal='SIGKILL'. Be aware that SIGKILL prevents cleanup code from running. - Confusing countdown with expires.
countdown=120delays execution by 120 seconds.expires=120discards the task if not started within 120 seconds. They serve different purposes and are often confused. - Flushing Redis FLUSHDB deletes everything. If your Redis instance stores more than Celery data (cache, sessions, rate limits),
FLUSHDBdestroys all of it. Target specific queue keys instead. - Not monitoring queue depth. Set up alerts for queue length thresholds (e.g., Prometheus + Flower). Discovering thousands of stuck tasks retroactively is much harder than preventing buildup.
Summary
- Use
app.control.purge()orcelery purgeto remove all pending tasks from the broker queue. - Use
app.control.revoke(task_id, terminate=True)to cancel specific tasks, including active ones. - Purging does not affect tasks already being executed or prefetched by workers. Restart workers after purging for a complete reset.
- For broker-level operations, use
redis-cli DELorrabbitmqctl purge_queuewhen Celery commands are unavailable. - Prevent task buildup proactively with
expires, rate limiting, and deduplication patterns. - Always inspect queue state with
celery inspectbefore and after purging to verify 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''
- ''Dense'' object has no attribute ''op''

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.