Skip to main content
The DeleteQueryBuilder is used to build and execute DELETE queries in Kysely. It provides a fluent API for deleting rows with type safety.

Basic Usage

Delete Rows

Delete rows from a table:
The generated SQL (PostgreSQL):

Delete with Multiple Conditions

Returning Data

On PostgreSQL and SQLite, you can use returning to get deleted rows:
The generated SQL (PostgreSQL):
Return all columns:

Using Clause (PostgreSQL, MySQL)

PostgreSQL: Using for Additional Tables

The using clause allows adding additional tables for filtering:
The generated SQL (PostgreSQL):

MySQL: Using with Joins

On MySQL, the using clause allows using joins:
The generated SQL (MySQL):

Multiple Tables in Using

The generated SQL (PostgreSQL):

Delete with Joins

You can join tables to delete based on related data:
See the joins documentation for more information on join methods.

Limit and Order By

Limit (MySQL)

Limit the number of deleted rows on MySQL:

Order By (MySQL)

Top Clause (MS SQL Server)

Delete the first N rows:
The generated SQL (MS SQL Server):
Delete a percentage of rows:
The generated SQL (MS SQL Server):

Where Conditions

All the standard where methods are available:
See the DeleteQueryBuilder documentation for more filtering options.

Clear Methods

Clear parts of the query:

API Reference

Main Methods

  • deleteFrom(table) - Specify the table to delete from
  • where(...) - Add WHERE conditions
  • whereRef(...) - Add WHERE conditions comparing columns
  • using(table) - Add USING clause for additional tables
  • innerJoin(), leftJoin(), rightJoin(), fullJoin() - Join tables
  • orderBy(...) - Order rows before deletion (MySQL)
  • limit(n) - Limit number of deleted rows (MySQL)
  • top(n) - Delete top N rows (MS SQL Server)
  • returning(...) - Return columns from deleted rows
  • returningAll() - Return all columns from deleted rows
  • clearWhere() - Clear WHERE conditions
  • clearOrderBy() - Clear ORDER BY clause
  • modifyEnd(modifier) - Add custom SQL
  • execute() - Execute the query
  • executeTakeFirst() - Execute and return first result
  • executeTakeFirstOrThrow() - Execute and return first result or throw

Result Object

The return value is an instance of DeleteResult: