Delete
Learn how to delete data using tSQL's type-safe deleteFrom() method. From simple deletions to returning deleted data, master database DELETE operations in TypeScript with full type safety and autocomplete support.
The deleteFrom
method allows you to delete rows from a table.
Basic Delete
The simplest way to delete rows is using the deleteFrom
method:
await db.deleteFrom("movies").execute();
Returning Deleted Data
On supported databases like PostgreSQL, you can return data from deleted rows:
const deletedMovie = await db
.deleteFrom("movies")
.where("id", "=", 123)
.returning(["id", "title"])
.executeTakeFirst();
// Returns: { id: 123, title: 'The Matrix' }
You can return all columns using returningAll()
:
const deletedMovie = await db
.deleteFrom("movies")
.where("id", "=", 123)
.returningAll()
.executeTakeFirst();
// Returns all columns from the deleted row
Getting Delete Count
You can get the number of deleted rows from the result:
const result = await db
.deleteFrom("movies")
.where("rating", "<", 5)
.executeTakeFirst();
console.log(`Deleted ${result.numDeletedRows} movies`);