HTTP

agent.destroy()

Destroys sockets currently managed by this agent. Use after the owner has finished its requests; destroying a shared agent during active work can interrupt requests. It is not a graceful application drain. Tested on Node.js 22.16.0. Example output (one line per entry): ["true", "true"].

Formal Syntax & Structure

nodejs syntaxnodejs
agent.destroy()

Return Value

undefined

No return value.

Working Example

Example Snippetnodejs
import http from 'node:http';const agent = new http.Agent({ keepAlive: true });const s = http.createServer((q, r) => r.end('ok'));await new Promise(r => s.listen(0, '127.0.0.1', r));try {    await new Promise((resolve, reject) => { http.get({ host: '127.0.0.1', port: s.address().port, agent }, r => { r.resume(); r.on('end', resolve); r.on('error', reject); }).on('error', reject); });    const sockets = Object.values(agent.freeSockets).flat().concat(Object.values(agent.sockets).flat());    console.log(sockets.length > 0);    agent.destroy();    console.log(sockets.every(x => x.destroyed));}finally {    agent.destroy();    s.closeAllConnections();    await new Promise(r => s.close(r));}
Node.js Reference