diff --git a/mvcc/backend/backend.go b/mvcc/backend/backend.go index 63a4a2e9998d..44d6055d311a 100644 --- a/mvcc/backend/backend.go +++ b/mvcc/backend/backend.go @@ -358,7 +358,14 @@ func (b *backend) defrag() error { b.batchTx.tx = nil - tmpdb, err := bolt.Open(b.db.Path()+".tmp", 0600, boltOpenOptions) + tmpdbPath := b.db.Path() + ".tmp" + if _, err := os.Stat(tmpdbPath); err == nil { + // Ensure we start with a clean slate since + // db.tmp files can be orphaned if etcd is terminated + // before defrag completes + os.Remove(tmpdbPath) + } + tmpdb, err := bolt.Open(tmpdbPath, 0600, boltOpenOptions) if err != nil { return err }