Sometimes it’s necessary to remove a job from the queue, such as when a job contains bad data or is no longer needed.
import { Queue } from 'bullmq';const queue = new Queue('paint');const job = await queue.add('wall', { color: 1 });await job.remove();
Locked jobs (in active state) cannot be removed. An error will be thrown if you attempt to remove a locked job.
From src/classes/job.ts:668:
/** * Completely remove the job from the queue. * Note, this call will throw an exception if the job * is being processed when the call is performed. * * @param opts - Options to remove a job */async remove({ removeChildren = true } = {}): Promise<void> { await this.queue.waitUntilReady(); const queue = this.queue; const job = this; const removed = await this.scripts.remove(job.id, removeChildren); if (removed) { queue.emit('removed', job); } else { throw new Error( `Job ${this.id} could not be removed because it is locked by another worker`, ); }}
// Remove immediately after failureawait queue.add('wall', { color: 'pink' }, { removeOnFail: true });// Keep only the last 50 failed jobsawait queue.add('wall', { color: 'blue' }, { removeOnFail: 50 });// Keep failed jobs for 7 days or max 500 jobsawait queue.add( 'wall', { color: 'green' }, { removeOnFail: { age: 7 * 24 * 3600, // seconds count: 500, }, },);
From src/interfaces/base-job-options.ts:46:
interface DefaultJobOptions { /** * If true, removes the job when it successfully completes * When given a number, it specifies the maximum amount of * jobs to keep, or you can provide an object specifying max * age and/or count to keep. It overrides whatever setting is used in the worker. * Default behavior is to keep the job in the completed set. */ removeOnComplete?: boolean | number | KeepJobs; /** * If true, removes the job when it fails after all attempts. * When given a number, it specifies the maximum amount of * jobs to keep, or you can provide an object specifying max * age and/or count to keep. It overrides whatever setting is used in the worker. * Default behavior is to keep the job in the failed set. */ removeOnFail?: boolean | number | KeepJobs;}
When removing a parent job with pending dependencies:
// Remove job and all its childrenawait parentJob.remove({ removeChildren: true });// Remove only the parent (children remain)await parentJob.remove({ removeChildren: false });
From src/classes/job.ts:692:
/** * Remove all children from this job that are not yet processed, * in other words that are in any other state than completed, failed or active. * * @remarks * - Jobs with locks (most likely active) are ignored. * - This method can be slow if the number of children is large (> 1000). */async removeUnprocessedChildren(): Promise<void> { const jobId = this.id; await this.scripts.removeUnprocessedChildren(jobId);}
If any child job is locked (active), the deletion process will be stopped.
/** * Removes child dependency from parent when child is not yet finished * * @returns True if the relationship existed and if it was removed. */async removeChildDependency(): Promise<boolean> { const childDependencyIsRemoved = await this.scripts.removeChildDependency( this.id, this.parentKey, ); if (childDependencyIsRemoved) { this.parent = undefined; this.parentKey = undefined; return true; } return false;}