Overview
Auto-removal is configured using theremoveOnComplete and removeOnFail options on individual jobs. These options can be set:
- When adding a job via
queue.add() - As default options on the queue instance
- As default options on the worker instance
Auto-removal works lazily. Jobs are not removed immediately, but when a new job completes or fails, triggering the cleanup process.
Remove All Finalized Jobs
The simplest option is to setremoveOnComplete or removeOnFail to true. Jobs will be removed automatically as soon as they finalize:
Keep a Certain Number of Jobs
Specify a maximum number of jobs to keep. A good practice is to keep a handful of completed jobs and a larger number of failed jobs for debugging:Keep Jobs Based on Age
Use theKeepJobs object to specify how long to keep jobs. You can combine age limits with count limits:
KeepJobs Options
Maximum age in seconds for jobs to be kept
Maximum count of jobs to be kept (used as a safeguard)
Maximum quantity of jobs to be removed per cleanup operation
Example: Age-Based Removal
count field acts as a safeguard: if you receive an unexpected burst of jobs, the count limit prevents memory issues by capping the total number of jobs kept.
Default Options on Queue
Set default removal options for all jobs added to a queue:Default Options on Worker
Workers can also define default removal options that override job-level settings:Worker-level settings override job-level settings. This is useful for enforcing consistent retention policies across all jobs.
Idempotence Considerations
One strategy for implementing idempotence with BullMQ is using unique job IDs. When you add a job with an ID that already exists, the new job is ignored and aduplicated event is triggered.
