Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly drop a database table content using Spring?

I'm using Spring-data-jpa with CrudRepository to persist data in a postgres db.

On a daily basis I want to renew the full db content, which means detele all data in a specific table first.

The repository offers a method dao.deleteAll(), but that lasts very long as I have about 500k entries.

How can I improve this?

like image 880
membersound Avatar asked Jul 18 '26 02:07

membersound


1 Answers

You can execute native sql query on entity manager, and truncate your database

String sql = "TRUNCATE tbl_name;";
entityManager.createNativeQuery(sql).executeUpdate();
like image 146
evilkyro Avatar answered Jul 19 '26 18:07

evilkyro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!