I'm using Slick 3.0 to access a PostgreSQL database in Play 2.4 and need a way to delete all rows from a table without dropping it.
Deleting single rows works:
lazy val tasks = TableQuery[Tasks]
def delete(id: Long) = db.run(tasks.filter(_.id === id).delete)
But I can't figure out how to implement an empty where clause in slick to delete all rows. (As seen in PostgreSQL documentation)
The trivial solution db.run(tasks.delete)
does not seem to do anything and there doesn't seem to be a truncate
function available.
Edit:
db.run(tasks.delete)
works, you just have to wait for the Future to complete. Truncating a table requires some SQL: db.run(sqlu"TRUNCATE TABLE table_name RESTART IDENTITY;")
.
db.run
returns an future you have to wait for it to complete – jilen