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?
Asked
Active
Viewed 1.0k times
9
-
1Tutorial: [Deleting a File or Directory](https://docs.oracle.com/javase/tutorial/essential/io/delete.html) – Gord Thompson Sep 04 '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 '18 at 15:40
2 Answers
8
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
-
this does not work, try creating table then call this method and again try to create the table with the same name, it will still throw an error as the table is already exists – nomadSK25 Apr 21 '20 at 18:58
1
Statement s = connection.createStatement();
s.execute("drop all objects delete files");

user939857
- 377
- 5
- 19