Finite scripts
A command-line or migration script finishes its database work and must release resources before exiting.
Prisma · Module
$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.
$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.
await prisma.$disconnect();Promise<void>
beforeExit hook, allowing shutdown-related work to run.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.
A command-line or migration script finishes its database work and must release resources before exiting.
A temporary PrismaClient is created for a bounded task in a process that remains alive.
A Cloudflare Worker schedules cleanup after producing a response.
$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.
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.
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 hookPrismaClientconnection poolconnection management