# Purge Queue (/sdks/nodejs/how-to/management/purge-queue)



## Overview [#overview]

Purging a queue is a management-plane operation for wiping a channel's backlog without receiving and discarding messages one at a time. Reach for it when a bad producer floods a channel, when you need a clean slate between test runs, or when you're resetting a queue during a maintenance window — all without deleting and recreating the channel itself.

`purgeQueue(channel)` tells the broker directly to acknowledge and drop every message still pending on the channel, entirely server-side. It only needs the channel name; the broker settles any in-flight deliveries and clears the backlog in one call, which is far cheaper than draining a large queue by looping receive-and-ack calls from the client.

**Gotchas:** the purge is irreversible — there's no undo once messages are dropped. It only reaches messages still waiting in the queue; anything already delivered to and held by an active consumer is untouched, so a purge run right after a receive can still leave stragglers. And purging empties the channel, it doesn't delete it — new messages can be sent immediately afterward.

## Prerequisites [#prerequisites]

* KubeMQ server running on `localhost:50000`
* Node.js SDK installed (`npm install kubemq-js`)

## Code [#code]

```typescript title="purge-queue.ts"
import { KubeMQClient, createQueueMessage } from 'kubemq-js';

async function main() {
  const client = await KubeMQClient.create({
    address: 'localhost:50000',
    clientId: 'js-management-purge-queue-client',
  });

  try {
    for (let i = 1; i <= 5; i++) {
      await client.sendQueueMessage(
        createQueueMessage({ channel: 'js-management.purge-queue', body: `msg-${i}` }),
      );
    }
    console.log('Sent 5 messages');

    await client.purgeQueue('js-management.purge-queue');
    console.log('Queue purged successfully');
  } finally {
    await client.close();
  }
}

main().catch(console.error);

```

## How It Works [#how-it-works]

* `client.purgeQueue(channel)` deletes all messages currently waiting in the queue; in-flight messages held by active consumers are not affected.
* The example first enqueues 5 messages to give the purge operation something to act on, then calls `purgeQueue()` to demonstrate the before/after state.
* Purge is non-reversible — use it for test cleanup, clearing poison-message accumulation, or resetting a queue during maintenance windows.
* The queue channel itself is not deleted; new messages can be sent immediately after purge without recreating the channel.

## Related [#related]

* [Node.js SDK Reference](/sdks/nodejs/reference)
* [Create Channel](/sdks/nodejs/how-to/management/create-channel)
* [Delete Channel](/sdks/nodejs/how-to/management/delete-channel)
