Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 53599da

Browse files
committed
Restore destroy() fix for Windows
1 parent c6957d0 commit 53599da

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

leveldown.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
33
const binding = require('./binding')
44
const ChainedBatch = require('./chained-batch')
55
const Iterator = require('./iterator')
6+
const fs = require('fs')
67

78
function LevelDOWN (location) {
89
if (!(this instanceof LevelDOWN)) {
@@ -121,7 +122,28 @@ LevelDOWN.destroy = function (location, callback) {
121122
throw new Error('destroy() requires a callback function argument')
122123
}
123124

124-
binding.destroy_db(location, callback)
125+
binding.destroy_db(location, function (err) {
126+
if (err) return callback(err)
127+
128+
// On Windows, RocksDB silently fails to remove the directory because its
129+
// Logger, which is instantiated on destroy(), has an open file handle on a
130+
// LOG file. Destroy() removes this file but Windows won't actually delete
131+
// it until the handle is released. This happens when destroy() goes out of
132+
// scope, which disposes the Logger. So back in JS-land, we can again
133+
// attempt to remove the directory. This is merely a workaround because
134+
// arguably RocksDB should not instantiate a Logger or open a file at all.
135+
fs.rmdir(location, function (err) {
136+
if (err) {
137+
// Ignore this error in case there are non-RocksDB files left.
138+
if (err.code === 'ENOTEMPTY') return callback()
139+
if (err.code === 'ENOENT') return callback()
140+
141+
return callback(err)
142+
}
143+
144+
callback()
145+
})
146+
})
125147
}
126148

127149
LevelDOWN.repair = function (location, callback) {

0 commit comments

Comments
 (0)