最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to delete all records in table without condition using knex? - Stack Overflow

programmeradmin3浏览0评论

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?

Share Improve this question edited Jul 21, 2021 at 8:37 jarlh 44.8k8 gold badges50 silver badges67 bronze badges asked Jul 21, 2021 at 8:25 BL4DERUNNERBL4DERUNNER 1953 silver badges15 bronze badges 1
  • 1 just write await knex('my_table').del(); – Asad Jivani Commented Jul 21, 2021 at 13:35
Add a ment  | 

3 Answers 3

Reset to default 9

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')

I think what are you looking for is truncate(). It does exactly what you've asked for:

await knex('table_name').truncate()
发布评论

评论列表(0)

  1. 暂无评论