# Deduplication

Sometimes, you may want to decide when you want to stop deduplicating jobs.

## Until job is active

As soon as job is moved to active, you must call **removeDeduplicationKey** method:

```typescript
import { Job, Queue, Worker } from 'bullmq';

const myQueue = new Queue('Paint');

const worker = new Worker('Paint', async (job: Job) => {
  await job.removeDeduplicationKey();
  console.log('Do something with job');
  return 'some value';
});

myQueue.add('house', { color: 'white' }, { deduplication: { id: 'house' } });
```

{% hint style="info" %}
Previous example uses [Simple Mode](/guide/jobs/deduplication.md#simple-mode) but it can be combined with [Throttle Mode](/guide/jobs/deduplication.md#throttle-mode) or [Debounce Mode](/guide/jobs/deduplication.md#debounce-mode).
{% endhint %}

## Using job schedulers

Sometimes it is desired to deduplicate jobs that are generated by job schedulers to save resources and avoid unnecessary work:

Deduplication options are not available in [`JobSchedulerTemplateOptions`](https://api.docs.bullmq.io/types/v5.JobSchedulerTemplateOptions.html) because they could interfere with job creation from scheduler templates. Since a new job is added as soon as the previous one moves to the active state, deduplication could disrupt this process by preventing the addition of this new record. However, there is an alternative to handle this. Let's look at an example:

```typescript
import { Queue, Worker } from 'bullmq';

const myQueue = new Queue('Paint');

const worker = new Worker(
  'Paint',
  async job => {
    if (job.name === 'paint-trigger') {
      // Add a job that will be deduplicated for 90 seconds.
      await myQueue.add(
        'house',
        { color: 'white' },
        { deduplication: { id: 'customValue', ttl: 90000 } },
      );
    }
  },
  { connection },
);

await myQueue.upsertJobScheduler('repeat', {
  pattern: '* * * * *', // every minute
  template: {
    name: 'paint-trigger',
    data: {},
  },
});
```

In this way, you can deduplicate a job when using job schedulers.

## Read more:

* 💡 [Add Job API Reference](https://api.docs.bullmq.io/classes/v5.Queue.html#add)
* 💡 [Deduplication Reference](/guide/jobs/deduplication.md)
* 💡 [Remove Deduplication Key API Reference](https://api.docs.bullmq.io/classes/v5.Job.html#removededuplicationkey)
* 💡 [Upsert Job Scheduler API Reference](https://api.docs.bullmq.io/classes/v5.Queue.html#upsertJobScheduler)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bullmq.io/patterns/deduplication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
