{"record":{"id":"b8b033f2c0a2b05b","repo":"NationalSecurityAgency/ghidra","slug":"failed-to-delete-h2-file-database","errorCode":null,"errorMessage":"failed to delete H2-file database: {}","messagePattern":"failed to delete H2-file database: (.+?)","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/file/H2FileFunctionDatabase.java","lineNumber":159,"sourceCode":"\t\t\ttry (ResultSet rs = st.executeQuery(\n\t\t\t\t\"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name\")) {\n\t\t\t\twhile (rs.next()) {\n\t\t\t\t\ttableNames.add(rs.getString(1));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Spot check for a few BSim table names that always exist\n\t\tif (!tableNames.contains(\"keyvaluetable\") || !tableNames.contains(\"desctable\") ||\n\t\t\t!tableNames.contains(\"weighttable\")) {\n\t\t\tthrow new SQLException(\"attempted to drop non-BSim database\");\n\t\t}\n\n\t\tfileDs.dispose(); // disconnect before deleting database\n\n\t\tBSimServerInfo serverInfo = fileDs.getServerInfo();\n\t\tif (!fileDs.delete()) {\n\t\t\tthrow new SQLException(\"failed to delete H2-file database: \" + serverInfo);\n\t\t}\n\n\t\tMsg.info(this, \"Deleted BSim H2-file database: \" + serverInfo);\n\n\t}\n\n\t/**\n\t * Create vector map which maps vector ID to {@link VectorStoreEntry}\n\t * @return vector map\n\t * @throws SQLException if error occurs while reading map data\n\t */\n\tpublic Map<Long, VectorStoreEntry> readVectorMap() throws SQLException {\n\t\treturn vectorTable.readVectors();\n\t}\n\n\t@Override\n\tprotected int deleteVectors(long id, int countdiff) throws SQLException {\n\t\treturn vectorTable.deleteVector(id, countdiff);","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/file/H2FileFunctionDatabase.java#L141-L177","documentation":"Thrown as an SQLException by H2FileFunctionDatabase.dropDatabase() when fileDs.delete() returns false after the database has been verified as a valid BSim database and the data source has been disposed. The H2 file data source was unable to delete the underlying database files from disk.","triggerScenarios":"Occurs when the BSim tables are confirmed present, fileDs.dispose() succeeds (disconnecting the pool), but fileDs.delete() returns false. This happens due to: OS-level file locking by another process, insufficient file system permissions, read-only file system, or the file being held open by an external process (antivirus, backup, IDE).","commonSituations":"Antivirus or backup software is scanning the database file. Another process (different JVM, file manager) has the file open. Running on a read-only or permission-restricted file system. The H2 lock file wasn't properly cleaned up. Network-mounted file system with delayed lock release.","solutions":["Check for processes holding the file open (lsof on Linux, Resource Monitor on Windows) and close them.","Verify file system write/delete permissions on the database directory.","Remove any stale H2 lock files (.lock.db, .lock.db.trace.db) manually after ensuring no process is active.","Retry the drop after a brief delay if a transient OS lock (backup/AV scan) is the cause.","As a last resort, shut down all processes and manually delete the database files from the OS."],"exampleFix":"// before\ndb.dropDatabase(); // delete() returns false\n\n// after\ntry {\n    db.dropDatabase();\n} catch (SQLException e) {\n    if (e.getMessage().contains(\"failed to delete\")) {\n        // clear stale locks and retry\n        Files.deleteIfExists(dbPath.resolve(\"bsim.lock.db\"));\n        Thread.sleep(1000);\n        Files.deleteIfExists(dbPath);\n    } else throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Check file writability/deletability before drop\nif (!Files.isWritable(dbPath)) {\n    throw new IllegalStateException(\n        \"Database file is not writable/deletable: \" + dbPath);\n}\n// Check no external process holds the file (Linux)\n// (platform-specific; on Linux use lsof, on Windows use Resource Monitor)","typeGuard":null,"tryCatchPattern":"try {\n    database.dropDatabase();\n} catch (SQLException e) {\n    if (e.getMessage().startsWith(\"failed to delete H2-file database:\")) {\n        // Clear locks and retry with OS-level deletion\n        Path lockFile = dbPath.resolveSibling(dbPath.getFileName() + \".lock.db\");\n        Files.deleteIfExists(lockFile);\n        Files.deleteIfExists(dbPath);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Ensure no antivirus, backup, or file manager has the database file open.","Verify file system permissions allow deletion before calling dropDatabase().","Close all connections and dispose the data source before deletion.","On network file systems, account for delayed lock release with retry logic."],"tags":["bsim","h2","database","drop","file-system","permissions","locked"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}