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?
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?
-
1
just write
await knex('my_table').del();
– Asad Jivani Commented Jul 21, 2021 at 13:35
3 Answers
Reset to default 9If 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')
I think what are you looking for is truncate()
. It does exactly what you've asked for:
await knex('table_name').truncate()