dgraph-io/badger · error
ErrGCInMemoryMode
ErrGCInMemoryMode
Error message
Cannot run value log GC when DB is opened in InMemory mode
What it means
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).
Source
Thrown at errors.go:110
// ErrBlockedWrites is returned if the user called DropAll. During the process of dropping all
// data from Badger, we stop accepting new writes, by returning this error.
ErrBlockedWrites = stderrors.New("Writes are blocked, possibly due to DropAll or Close")
// ErrNilCallback is returned when subscriber's callback is nil.
ErrNilCallback = stderrors.New("Callback cannot be nil")
// ErrEncryptionKeyMismatch is returned when the storage key is not
// matched with the key previously given.
ErrEncryptionKeyMismatch = stderrors.New("Encryption key mismatch")
// ErrInvalidDataKeyID is returned if the datakey id is invalid.
ErrInvalidDataKeyID = stderrors.New("Invalid datakey id")
// ErrInvalidEncryptionKey is returned if length of encryption keys is invalid.
ErrInvalidEncryptionKey = stderrors.New("Encryption key's length should be" +
"either 16, 24, or 32 bytes")
// ErrGCInMemoryMode is returned when db.RunValueLogGC is called in in-memory mode.
ErrGCInMemoryMode = stderrors.New("Cannot run value log GC when DB is opened in InMemory mode")
// ErrGCInReadOnlyMode is returned when db.RunValueLogGC is called in read-only mode.
ErrGCInReadOnlyMode = stderrors.New("Cannot run value log GC when DB is opened in ReadOnly mode")
// ErrDBClosed is returned when a get operation is performed after closing the DB.
ErrDBClosed = stderrors.New("DB Closed")
)
View on GitHub (pinned to 2a001d466f)
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.
Example fix
// before
if err := db.RunValueLogGC(0.5); err != nil {
return err
}
// after
if !opts.InMemory {
if err := db.RunValueLogGC(0.5); err != nil && !errors.Is(err, badger.ErrGCInMemoryMode) {
return err
}
} Defensive patterns
Strategy: validation
Validate before calling
func shouldRunGC(db *badger.DB, inMemory bool) bool {
return !inMemory // GC is only valid for on-disk DBs
}
// call site:
if !inMemory {
if err := db.RunValueLogGC(0.5); err != nil { ... }
} Type guard
func canRunValueLogGC(opt badger.Options) bool {
return !opt.InMemory && !opt.ReadOnly
} Try / catch
if err := db.RunValueLogGC(0.5); err != nil {
if errors.Is(err, badger.ErrGCInMemoryMode) {
return nil // expected for in-memory DBs, treat as no-op
}
return err
} Prevention
- 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.
When it happens
Trigger: Calling db.RunValueLogGC(discardRatio) on a DB opened via badger.Open(badger.DefaultOptions("").WithInMemory(true)) — the call fails immediately before any discard-ratio checks.
Common situations: 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.
Related errors
- ErrGCInReadOnlyMode
- Cannot use badger in Disk-less mode with Dir or ValueDir set
- ErrValueLogSize
- ErrThresholdZero
- ErrNamespaceMode
AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05).
Data as JSON: /api/errors/a869ed1ed2b272ca.
Report an issue: GitHub.