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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With