{"record":{"id":"7485c8ad4da5db13","repo":"dgraph-io/badger","slug":"file-s-already-exists","errorCode":null,"errorMessage":"File %s already exists","messagePattern":"File (.+?) already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"memtable.go","lineNumber":148,"sourceCode":"\tif lerr == z.NewFile {\n\t\treturn mt, lerr\n\t}\n\terr := mt.UpdateSkipList()\n\treturn mt, y.Wrapf(err, \"while updating skiplist\")\n}\n\nfunc (db *DB) newMemTable() (*memTable, error) {\n\tmt, err := db.openMemTable(db.nextMemFid, os.O_CREATE|os.O_RDWR)\n\tif err == z.NewFile {\n\t\tdb.nextMemFid++\n\t\treturn mt, nil\n\t}\n\n\tif err != nil {\n\t\tdb.opt.Errorf(\"Got error: %v for id: %d\\n\", err, db.nextMemFid)\n\t\treturn nil, y.Wrapf(err, \"newMemTable\")\n\t}\n\treturn nil, fmt.Errorf(\"File %s already exists\", mt.wal.Fd.Name())\n}\n\nfunc (db *DB) mtFilePath(fid int) string {\n\treturn filepath.Join(db.opt.Dir, fmt.Sprintf(\"%05d%s\", fid, memFileExt))\n}\n\nfunc (mt *memTable) SyncWAL() error {\n\treturn mt.wal.Sync()\n}\n\nfunc (mt *memTable) isFull() bool {\n\tif mt.sl.MemSize() >= mt.opt.MemTableSize {\n\t\treturn true\n\t}\n\tif mt.opt.InMemory {\n\t\t// InMemory mode doesn't have any WAL.\n\t\treturn false\n\t}","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/memtable.go#L130-L166","documentation":"newMemTable creates the next memtable WAL file; this error is returned when a file with the name that the new memtable would use (based on db.nextMemFid) already exists on disk. Normally the rotate path expects the file to not exist, so an existing file at that id indicates out-of-sync fid state or leftover files.","triggerScenarios":"Memtable rotation via ensureRoomForWrite, dropAll, DropPrefix, or Open when a WAL file with the next memtable fid already exists in the directory (e.g. crash leftovers, manually added files, or fid counters reset while old files remain).","commonSituations":"Restoring a subset of files into the data dir so ids collide; running two badger instances against the same directory; older/patched builds whose fid numbering differs from files left on disk.","solutions":["Stop all processes using the directory; ensure only one badger instance opens it","Compare db.nextMemFid against existing %05d.clog files; remove stale leftover WAL files that were never fully committed (after confirming they're not referenced in MANIFEST)","Restore the full consistent backup set of files rather than a partial copy","Rebuild the DB (replay from backup or re-ingest) if the directory state is inconsistent"],"exampleFix":"// before: two processes on same dir\n//   db1, _ := badger.Open(opt) // in service A\n//   db2, _ := badger.Open(opt) // in service B -> 'File 000005.clog already exists'\n// after: single owner, or a lock/coordination layer\nif !acquiredDirLock(dir) {\n    return errors.New(\"another badger instance owns this directory\")\n}","handlingStrategy":"validation","validationCode":"// before opening, ensure no competing instance and that next-fid collisions are unlikely:\nfiles, _ := os.ReadDir(dir)\nids := map[int]bool{}\nfor _, f := range files {\n    var id int\n    if n, err := fmt.Sscanf(f.Name(), \"%05d\", &id); err == nil && n == 1 {\n        ids[id] = true\n    }\n}\n_ = ids // investigate gaps/leftovers inconsistent with MANIFEST before open","typeGuard":"func singleOwner(dir string) bool {\n    return os.Mkdir(filepath.Join(dir, \".lock\"), 0o600) == nil // O_EXCL-style lock dir\n}","tryCatchPattern":"db, err := badger.Open(opt)\nif err != nil && strings.Contains(err.Error(), \"already exists\") {\n    return fmt.Errorf(\"badger dir %s may be in use by another process or has stale WAL files: %w\", opt.Dir, err)\n}","preventionTips":["Guarantee a single badger writer per directory (flock or orchestrator leadership)","Restore complete backup sets, never partial file copies","Clean up only after confirming leftover WAL files are unreferenced by MANIFEST"],"tags":["badger","memtable","file-conflict","startup"],"backgroundTag":"file-already-exists","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}