{"record":{"id":"a869ed1ed2b272ca","repo":"dgraph-io/badger","slug":"errgcinmemorymode","errorCode":"ErrGCInMemoryMode","errorMessage":"Cannot run value log GC when DB is opened in InMemory mode","messagePattern":"Cannot run value log GC when DB is opened in InMemory mode","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":110,"sourceCode":"\t// ErrBlockedWrites is returned if the user called DropAll. During the process of dropping all\n\t// data from Badger, we stop accepting new writes, by returning this error.\n\tErrBlockedWrites = stderrors.New(\"Writes are blocked, possibly due to DropAll or Close\")\n\n\t// ErrNilCallback is returned when subscriber's callback is nil.\n\tErrNilCallback = stderrors.New(\"Callback cannot be nil\")\n\n\t// ErrEncryptionKeyMismatch is returned when the storage key is not\n\t// matched with the key previously given.\n\tErrEncryptionKeyMismatch = stderrors.New(\"Encryption key mismatch\")\n\n\t// ErrInvalidDataKeyID is returned if the datakey id is invalid.\n\tErrInvalidDataKeyID = stderrors.New(\"Invalid datakey id\")\n\n\t// ErrInvalidEncryptionKey is returned if length of encryption keys is invalid.\n\tErrInvalidEncryptionKey = stderrors.New(\"Encryption key's length should be\" +\n\t\t\"either 16, 24, or 32 bytes\")\n\t// ErrGCInMemoryMode is returned when db.RunValueLogGC is called in in-memory mode.\n\tErrGCInMemoryMode = stderrors.New(\"Cannot run value log GC when DB is opened in InMemory mode\")\n\n\t// ErrGCInReadOnlyMode is returned when db.RunValueLogGC is called in read-only mode.\n\tErrGCInReadOnlyMode = stderrors.New(\"Cannot run value log GC when DB is opened in ReadOnly mode\")\n\n\t// ErrDBClosed is returned when a get operation is performed after closing the DB.\n\tErrDBClosed = stderrors.New(\"DB Closed\")\n)\n","sourceCodeStart":92,"sourceCodeEnd":118,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/errors.go#L92-L118","documentation":"ErrGCInMemoryMode is a sentinel error returned by DB.RunValueLogGC when the badger DB was opened with the InMemory option set to true. Value log garbage collection fundamentally requires reading and truncating value log files on disk, which do not exist in in-memory mode, so GC is meaningless and the call is rejected upfront. It is declared in errors.go and checked as the very first condition in RunValueLogGC (db.go:1306).","triggerScenarios":"Calling db.RunValueLogGC(discardRatio) on a DB opened via badger.Open(badger.DefaultOptions(\"\").WithInMemory(true)) — the call fails immediately before any discard-ratio checks.","commonSituations":"Applications that run badger purely in RAM (caching, ephemeral workloads) and then add generic maintenance routines that unconditionally call RunValueLogGC; copy-pasted GC cron jobs from disk-based deployments applied to in-memory instances.","solutions":["Do not call RunValueLogGC when the DB is in InMemory mode; skip GC in your maintenance code.","Gate the GC call on the same options used at Open time (keep a flag or expose db.Opts.InMemory).","If GC is genuinely needed, reopen the DB on disk instead of InMemory mode.","Handle the error explicitly: if errors.Is(err, badger.ErrGCInMemoryMode), treat as a no-op, not a failure."],"exampleFix":"// before\nif err := db.RunValueLogGC(0.5); err != nil {\n    return err\n}\n// after\nif !opts.InMemory {\n    if err := db.RunValueLogGC(0.5); err != nil && !errors.Is(err, badger.ErrGCInMemoryMode) {\n        return err\n    }\n}","handlingStrategy":"validation","validationCode":"func shouldRunGC(db *badger.DB, inMemory bool) bool {\n    return !inMemory // GC is only valid for on-disk DBs\n}\n// call site:\nif !inMemory {\n    if err := db.RunValueLogGC(0.5); err != nil { ... }\n}","typeGuard":"func canRunValueLogGC(opt badger.Options) bool {\n    return !opt.InMemory && !opt.ReadOnly\n}","tryCatchPattern":"if err := db.RunValueLogGC(0.5); err != nil {\n    if errors.Is(err, badger.ErrGCInMemoryMode) {\n        return nil // expected for in-memory DBs, treat as no-op\n    }\n    return err\n}","preventionTips":["Keep the badger.Options used at Open available (or a bool flag) wherever maintenance code runs.","Centralize GC in one maintenance function that checks mode before calling RunValueLogGC.","Never copy GC cron jobs unconditionally between disk-based and in-memory deployments.","Use errors.Is against badger.ErrGCInMemoryMode rather than string matching."],"tags":["badger","in-memory","value-log-gc","configuration"],"backgroundTag":"gc-not-supported-in-memory","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"}