How to delete a H2 database file programmatically?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to delete a H2 database file programmatically?



I'm developing an application using JDBC and an H2 database, and occasionally there is a need to delete the database file. Is there a way to do that?





Tutorial: Deleting a File or Directory
– Gord Thompson
Sep 4 '16 at 12:10





By the way, if you need a temporary database without persistence, H2 supports “in-memory” mode where the database lives only in memory, never gets written to storage, and disappears when your app exits.
– Basil Bourque
Aug 12 at 15:40




2 Answers
2



Yes, you can!



Refer to this answer to locate the folder where H2 stores the database (usually user's home directory):
Where does H2's Embedded Databases Store the data?



To delete it, you can use the org.h2.tools.DeleteDbFiles class as follows:


DeleteDbFiles.execute(dbDir, dbName, true);



More info about DeleteDbFiles class: http://www.h2database.com/javadoc/org/h2/tools/DeleteDbFiles.html


Statement s = connection.createStatement();
s.execute("drop all objects delete files");






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard