How to delete all records in table without condition using knex?

Viewed 2053

Question is kinda simple. I want to delete all rows from my table using knex, but without conditions.

await knex('my_table')
  .del().where()

Method .del() uses condition. Is it possible? Or is there any SQL syntax to do that?

2 Answers

If you want to delete all rows, you just need to use .del() without any where condition.

await knex('my_table').del()

Note that it will not delete the table. It will delete all the rows in the table.

So, it's really easy, just add condition, which always will be true:

await knex('my_table')
  .del().where('id', '!=', 'null')
Related