dgraph-io/badger · error

Cannot use badger in Disk-less mode with Dir or ValueDir set

Error message

Cannot use badger in Disk-less mode with Dir or ValueDir set

What it means

This error is raised by checkAndSetOptions (db.go:152) during badger.Open when InMemory is true but Dir or ValueDir is also set to a non-empty path. In-memory mode is disk-less by definition: any configured directory contradicts the option and would otherwise be ignored or misused, so Open rejects the combination eagerly. It is an ad-hoc errors.New string, not a sentinel.

Source

Thrown at db.go:152

	blockCache *ristretto.Cache[[]byte, *table.Block]
	indexCache *ristretto.Cache[uint64, *fb.TableIndex]
	allocPool  *z.AllocatorPool
}

const (
	kvWriteChCapacity = 1000
)

func checkAndSetOptions(opt *Options) error {
	// It's okay to have zero compactors which will disable all compactions but
	// we cannot have just one compactor otherwise we will end up with all data
	// on level 2.
	if opt.NumCompactors == 1 {
		return errors.New("Cannot have 1 compactor. Need at least 2")
	}

	if opt.InMemory && (opt.Dir != "" || opt.ValueDir != "") {
		return errors.New("Cannot use badger in Disk-less mode with Dir or ValueDir set")
	}
	opt.maxBatchSize = (15 * opt.MemTableSize) / 100
	opt.maxBatchCount = opt.maxBatchSize / int64(skl.MaxNodeSize)

	// This is the maximum value, vlogThreshold can have if dynamic thresholding is enabled.
	opt.maxValueThreshold = math.Min(maxValueThreshold, float64(opt.maxBatchSize))
	if opt.VLogPercentile < 0.0 || opt.VLogPercentile > 1.0 {
		return errors.New("vlogPercentile must be within range of 0.0-1.0")
	}

	// We are limiting opt.ValueThreshold to maxValueThreshold for now.
	if opt.ValueThreshold > maxValueThreshold {
		return fmt.Errorf("Invalid ValueThreshold, must be less or equal to %d",
			maxValueThreshold)
	}

	// If ValueThreshold is greater than opt.maxBatchSize, we won't be able to push any data using
	// the transaction APIs. Transaction batches entries into batches of size opt.maxBatchSize.

View on GitHub (pinned to 2a001d466f)

Solutions

  1. When InMemory is true, build options with badger.DefaultOptions("") so Dir and ValueDir stay empty.
  2. Conditionally clear the directories: if inMemory, set Dir="" and ValueDir="" before Open.
  3. Make the in-memory toggle and the directory setting mutually exclusive in your configuration layer, validated before constructing badger.Options.
  4. If persistence is actually needed, remove WithInMemory(true) instead and keep the paths.

Example fix

// before
opts := badger.DefaultOptions(dir).WithInMemory(true) // fails: Disk-less mode with Dir set
// after
if inMemory {
    opts = badger.DefaultOptions("").WithInMemory(true)
} else {
    opts = badger.DefaultOptions(dir)
}
db, err := badger.Open(opts)
Defensive patterns

Strategy: validation

Validate before calling

if opts.InMemory && (opts.Dir != "" || opts.ValueDir != "") {
    return errors.New("badger: InMemory mode requires empty Dir and ValueDir")
}
db, err := badger.Open(opts)

Type guard

func optionsConsistent(opt badger.Options) bool {
    return !opt.InMemory || (opt.Dir == "" && opt.ValueDir == "")
}

Try / catch

db, err := badger.Open(opts)
if err != nil {
    if strings.Contains(err.Error(), "Disk-less mode") {
        return fmt.Errorf("badger config conflict: InMemory=true with Dir=%q ValueDir=%q", opts.Dir, opts.ValueDir)
    }
    return err
}

Prevention

When it happens

Trigger: badger.Open with Options where InMemory=true and either Dir != "" or ValueDir != "" — commonly DefaultOptions("/some/path").WithInMemory(true), because DefaultOptions(path) pre-fills Dir/ValueDir.

Common situations: Starting from DefaultOptions(dir) for a disk deployment and flipping on WithInMemory(true) to test, forgetting the leftover path; environment-driven config where dir is always populated but an in-memory toggle was added later; test fixtures sharing one options builder.

Related errors


AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05). Data as JSON: /api/errors/46866c5aeae10f68. Report an issue: GitHub.