Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Module

$disconnect

$disconnect() asynchronously releases Prisma Client's database connections and shuts down its query engine. Use it mainly for finite scripts or temporary clients, not after every request in a long-running application.

How it works

$disconnect() asynchronously shuts down Prisma Client's database connections and query engine after work is complete. It is mainly useful for temporary clients and finite scripts; continuously serving applications should normally keep the client and its pool available instead of disconnecting after every request.

Syntax

await prisma.$disconnect();

Returns

Promise<void>

Key points

  • The method releases connections opened through Prisma Client's connection lifecycle.
  • Calling it first triggers Prisma's beforeExit hook, allowing shutdown-related work to run.
  • After the hook, Prisma closes every connection held by its pool.
  • A normal process exit automatically causes Prisma Client to disconnect.
  • Keep the client available across requests so its connections can be reused.

Common mistakes

  • Do not disconnect after every request in a long-running service: reconnecting repeatedly adds latency and prevents connection-pool reuse.
  • Await the returned promise so shutdown completes before the surrounding process or task ends.
  • A long-running process that does not terminate may need an explicit disconnect when a temporary client is no longer needed.

Example: Disconnect after a script finishes

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
  await prisma.$connect();
  const users = await prisma.user.findMany();
  console.log(users);
}

main()
  .catch(console.error)
  .finally(async () => {
    await prisma.$disconnect();
  });

Runs a query and releases Prisma Client's connections when the script finishes.

When to use it

Finite scripts

A command-line or migration script finishes its database work and must release resources before exiting.

Temporary clients

A temporary PrismaClient is created for a bounded task in a process that remains alive.

Serverless resource release

A Cloudflare Worker schedules cleanup after producing a response.

Interview questions

QWhat does $disconnect() do?

$disconnect() closes the database connections established by Prisma Client and stops the process running Prisma ORM's query engine. It returns a promise, so it should be awaited.

Follow-up: When should it be avoided? In continuously serving applications, avoid calling it after each request because establishing connections again slows request handling and defeats pool reuse.

QWhen is explicit disconnection unnecessary?

Most applications do not need explicit connection lifecycle calls. Prisma Client connects automatically on the first query and disconnects when the Node.js process ends.

Follow-up: Why can a temporary client still need it? If a client is created inside a long-lived process and the process does not shut down, explicit cleanup prevents its pool from remaining open.

QGive a situation where explicit disconnection is appropriate.

Use $disconnect() for finite scripts or temporary clients after their work is complete. It is also appropriate when releasing resources explicitly in environments such as Cloudflare Workers.

Follow-up: What is the Cloudflare Workers pattern? Schedule prisma.$disconnect() with ctx.waitUntil(...) so cleanup can proceed while the response is returned.

  • $connect()
  • beforeExit hook
  • PrismaClient
  • connection pool
  • connection management

Source: Connection management. Checked 2026-10-16.