emitter.removeAllListeners()

Remove every listener for one event, or for all events when called with no argument.

Signature
emitter.removeAllListeners([eventName])
  • Added in Node.js v0.1.26

Parameters

ParameterTypeDescription
$eventNameoptional<string> | <symbol>The event to clear. Omit it to remove listeners for every event on the emitter.

Return value

<EventEmitter>A reference to the emitter, so calls can be chained.

Errors

Does not throw for unknown event names; clearing an event nobody subscribed to is a no-op.

removeAllListeners() clears the listener array for eventName, or every listener array on the emitter when the argument is omitted. It is the sledgehammer version of off().

When to reach for it

Use it when you own the emitter and are tearing it down: disposing a connection wrapper, resetting a test double between cases, or cleaning up an object whose subscribers are all yours. It is also the only practical escape hatch when a listener was registered as an inline arrow and no reference survives.

What trips people up

It removes listeners you did not add. Calling the no-argument form on a shared emitter — a stream, a socket, process — deletes core's own internal handlers along with yours, which silently breaks the object. The Node docs specifically call out that this is bad practice for emitters you did not create.

Clearing 'error' is the worst case: an emitter with no 'error' listener throws on the next error instead of reporting it, turning a handled failure into a crash. Prefer naming the event, and prefer off() when you have the reference.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Tear down a connection wrapper so no stale handler fires after disposal
  • Reset a shared mock emitter between test cases
  • Drop every subscriber to one event when a feature is disabled at runtime

Frequently asked questions

How do I remove all listeners from an event emitter in Node.js?

Call emitter.removeAllListeners() with no arguments to clear every event, or pass an event name to clear just that one. Prefer the named form: the no-argument version also removes internal listeners on emitters you did not create.

Is removeAllListeners() safe to call on a stream or socket?

Not without an event name. Streams, sockets and process register their own internal listeners, and clearing them breaks the object's state machine and removes its 'error' handling. Pass the specific event you subscribed to, or track your listeners and use off().

Does removeAllListeners() affect an emit that is already running?

No. emit() snapshots the listener array before it starts calling, so listeners removed from inside a handler still run for that emit. The removal takes effect from the next emit onwards.

More examples

Disposable wrapper around an emitter

'close' is emitted before the teardown so subscribers still get their final notification, then the emitter is emptied. The second emit prints nothing and eventNames() is empty — a disposed Session cannot accidentally hold a closure alive. This is safe only because Session owns the emitter outright.

Example · javascript
const { EventEmitter } = require('node:events');

class Session extends EventEmitter {
  constructor() {
    super();
    this.closed = false;
  }

  close() {
    this.closed = true;
    this.emit('close');
    // Safe: this emitter is ours, and nothing else subscribes to it.
    this.removeAllListeners();
  }
}

const s = new Session();
s.on('close', () => console.log('subscriber saw close'));
s.close();

console.log('listeners after close:', s.eventNames());
s.emit('close');
console.log('second emit reached nobody');

Why the no-argument form is dangerous on core objects

Streams register internal listeners for their own state machine. Wiping them all leaves the stream in an inconsistent state and, because the 'error' listener goes with them, the next I/O failure becomes an uncaught exception. Always pass an event name when the emitter came from core or a library.

Example · javascript
const fs = require('node:fs');

const stream = fs.createReadStream('package.json');
stream.on('data', (chunk) => console.log('bytes:', chunk.length));
stream.on('error', (err) => console.error('read failed:', err.message));

// DO NOT DO THIS on an emitter you did not create.
// It removes Node's own internal 'error' and 'end' handlers too,
// so a missing file now throws instead of reaching your handler.
// stream.removeAllListeners();

// Remove only what you added, by name:
stream.removeAllListeners('data');
console.log('remaining events:', stream.eventNames());

Discussion

  • Be the first to comment on this lesson.