{"record":{"id":"0eb7db1df206b40d","repo":"dgraph-io/badger","slug":"errgcinreadonlymode","errorCode":"ErrGCInReadOnlyMode","errorMessage":"Cannot run value log GC when DB is opened in ReadOnly mode","messagePattern":"Cannot run value log GC when DB is opened in ReadOnly mode","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":113,"sourceCode":"\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":95,"sourceCodeEnd":118,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/errors.go#L95-L118","documentation":"ErrGCInReadOnlyMode is a sentinel error returned by DB.RunValueLogGC when the badger DB was opened with the ReadOnly option set to true. Garbage collection rewrites and truncates value log files, a write operation that a read-only DB cannot perform, so the call is rejected immediately after the InMemory check in RunValueLogGC (db.go:1309).","triggerScenarios":"Calling db.RunValueLogGC(discardRatio) on a DB opened with badger.Open(opts.WithReadOnly(true)) — e.g. a replica or a database opened for inspection on read-only storage.","commonSituations":"Opening a badger directory on a read-only filesystem or a secondary/read-only replica for analytics, then reusing maintenance code that runs GC; test code like db_test.go:1794 that explicitly exercises this path.","solutions":["Skip RunValueLogGC entirely for DBs opened in ReadOnly mode.","Run value log GC on the writable primary instance instead.","If the directory must be writable again, reopen the DB without WithReadOnly(true).","Treat the error as a no-op: if errors.Is(err, badger.ErrGCInReadOnlyMode), log and continue."],"exampleFix":"// before\nerr := db.RunValueLogGC(0.5)\n// after\nif !opts.ReadOnly {\n    if err := db.RunValueLogGC(0.5); err != nil && !errors.Is(err, badger.ErrGCInReadOnlyMode) {\n        return err\n    }\n}","handlingStrategy":"validation","validationCode":"func shouldRunGC(db *badger.DB, readOnly bool) bool {\n    return !readOnly // GC needs write access to value log files\n}\n// call site:\nif !readOnly {\n    if err := db.RunValueLogGC(0.5); err != nil { ... }\n}","typeGuard":"func canRunValueLogGC(opt badger.Options) bool {\n    return !opt.ReadOnly && !opt.InMemory\n}","tryCatchPattern":"if err := db.RunValueLogGC(0.5); err != nil {\n    if errors.Is(err, badger.ErrGCInReadOnlyMode) {\n        return nil // expected on read-only DBs, skip GC\n    }\n    return err\n}","preventionTips":["Track the ReadOnly flag from options wherever GC maintenance is scheduled.","Run value log GC only on writable primaries, never on read-only replicas.","On read-only filesystems, treat all write-maintenance as skipped by design.","Use errors.Is against badger.ErrGCInReadOnlyMode instead of comparing messages."],"tags":["badger","read-only","value-log-gc","configuration"],"backgroundTag":"operation-not-permitted-readonly","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"}