Get started
Overview PostgreSQL MySQL SQLite Manage schema
Overview Column types Indexes & Constraints Sequences Migrations Views Schemas Extensions Access your data
Query Select Insert Update Delete Filters Joins Magic sql`` operator Performance
Queries Serverless Advanced
Set Operations Generated Columns Transactions Batch Dynamic query building Read Replicas Custom types Goodies Extensions
Prisma ESLint Plugin drizzle-zod drizzle-typebox drizzle-valibot drizzle-graphql SQL Delete
You can delete all rows in the table:
await db.delete(users);
And you can delete with filters and conditions:
await db.delete(users).where(eq(users.name, 'Dan'));
Delete with return
PostgreSQL
SQLite
MySQL
You can delete a row and get it back in PostgreSQL and SQLite:
const deletedUser = await db.delete(users)
.where(eq(users.name, 'Dan'))
.returning();
// partial return
const deletedUserIds: { deletedId: number }[] = await db.delete(users)
.where(eq(users.name, 'Dan'))
.returning({ deletedId: users.id });
WITH DELETE clause
Using the with
clause can help you simplify complex queries by splitting them into smaller subqueries called common table expressions (CTEs):
const averageAmount = db.$with('average_amount').as(
db.select({ value: sql`avg(${orders.amount})`.as('value') }).from(orders)
);
const result = await db
.with(averageAmount)
.delete(orders)
.where(gt(orders.amount, sql`(select * from ${averageAmount})`))
.returning({
id: orders.id
});
with "average_amount" as (select avg("amount") as "value" from "orders")
delete from "orders"
where "orders"."amount" > (select * from "average_amount")
returning "id"