onClose hook
Runs when the server is closing, allowing plugins to release resources.
Syntax
app.addHook('onClose', async (instance) => {}) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
instance | FastifyInstance | No | The instance being closed. |
Returns
void | Promise<void> — Awaited during shutdown.
Examples
import Fastify from 'fastify';
const app = Fastify();
const pool = createPool();
app.addHook('onClose', async () => {
await pool.end();
});
Notes
Triggered by app.close(). Use it to close database pools, flush buffers,
and clear timers for graceful shutdown. Plugins typically register their
own onClose to clean up encapsulated resources.