Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a H2 database file programmatically?

Tags:

java

jdbc

h2

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?

like image 701
dharan Avatar asked Sep 04 '16 04:09

dharan


People also ask

How do I delete data from my H2 database?

The SQL DELETE query is used to delete the existing records from a table. We can use WHERE clause with DELETE query to delete selected records, otherwise all the records will be deleted.

How do I access my H2 database table?

Accessing the H2 Console H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.


1 Answers

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

like image 57
cdr89 Avatar answered Oct 02 '22 22:10

cdr89