H2 - How to truncate all tables?

database, h2, sql

Solution

You may do it this way:

Disable referential integrity using `SET REFERENTIAL_INTEGRITY FALSE`

Get the list of all tables using `SHOW TABLES`

Delete the data from each table using `TRUNCATE TABLE tableName`

Enable referential integrity using `SET REFERENTIAL_INTEGRITY TRUE`

Problem

I assume there is a way of doing this from code, at least some good workaround. Please consider that I do not want to delete all tables (I've seen this command), just to delete rows from them but to keep existing schema and all constraints. Maybe I can somehow obtain the list of all tables from metadata and apply TRUNCATE command for each separately? But what about their relations and foreign keys? Any Idea?

Original source

Related problems