dgraph-io/badger · error

file with ID: %d not found

Error message

file with ID: %d not found

What it means

getFileRLocked looks up the value log file matching the value pointer's file ID (vp.Fid) in vlog.filesMap and returns this error when there is no such entry. It means the vlog file a key's valuePointer references is no longer known to this DB instance — deleted, never loaded, or the pointer is stale. Badger does not retry; it returns the error to the caller.

Source

Thrown at value.go:943

		vlog.db.threshold.update(valueSizes)
		// We write to disk here so that all entries that are part of the same transaction are
		// written to the same vlog file.
		if err := toDisk(); err != nil {
			return err
		}
	}
	return toDisk()
}

// Gets the logFile and acquires and RLock() for the mmap. You must call RUnlock on the file
// (if non-nil)
func (vlog *valueLog) getFileRLocked(vp valuePointer) (*logFile, error) {
	vlog.filesLock.RLock()
	defer vlog.filesLock.RUnlock()
	ret, ok := vlog.filesMap[vp.Fid]
	if !ok {
		// log file has gone away, we can't do anything. Return.
		return nil, fmt.Errorf("file with ID: %d not found", vp.Fid)
	}

	// Check for valid offset if we are reading from writable log.
	maxFid := vlog.maxFid
	// In read-only mode we don't need to check for writable offset as we are not writing anything.
	// Moreover, this offset is not set in readonly mode.
	if !vlog.opt.ReadOnly && vp.Fid == maxFid {
		currentOffset := vlog.woffset()
		if vp.Offset >= currentOffset {
			return nil, fmt.Errorf(
				"Invalid value pointer offset: %d greater than current offset: %d",
				vp.Offset, currentOffset)
		}
	}

	ret.lock.RLock()
	return ret, nil
}

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Ensure all value log files present at write time remain in the DB directory — never delete vlogs manually; let badger's GC manage them.
  2. Restore the complete DB directory (SSTs AND vlogs together) rather than a partial copy.
  3. If pointers are stale but data exists elsewhere, iterate-and-rewrite affected keys so valuePointers move to current files.
  4. If files are unrecoverable, rebuild via Stream/Backup into a fresh DB — the referenced values are gone.
  5. Confirm you opened the same directory the data was written to (correct dir paths, no volume mix-ups).

Example fix

// before: deleting old vlog files to free space
$ rm /data/badger/000001.vlog
// later: read fails: file with ID: 1 not found
// after: use badger's own GC
if err := db.RunValueLogGC(0.5); err != nil { /* handle */ }
// and restore full backups: include *.vlog and *.sst
Defensive patterns

Strategy: fallback

Validate before calling

// Go: sanity-check a valuePointer's file is known before reading
func vpFileKnown(vp badger.ValuePointer, known map[uint32]struct{}) bool {
    _, ok := known[vp.Fid]
    return ok
}

Type guard

func isFileNotFoundErr(err error) bool {
    return err != nil && strings.Contains(err.Error(), "file with ID") &&
        strings.Contains(err.Error(), "not found")
}

Try / catch

item, err := txn.Get(key)
if err != nil && isFileNotFoundErr(err) {
    // the value's vlog file is gone; treat as missing and regenerate
    log.Printf("stale value pointer for %s: %v", key, err)
    return regenerateValue(key)
}

Prevention

When it happens

Trigger: Reading a key whose valuePointer references a FID that was garbage-collected, a valuePointer from a different DB directory, stale SST entries after a crash where vlog files were lost, or manual deletion of *.vlog files.

Common situations: Restoring only SSTs but not all vlogs from backup; deleting vlog files to reclaim space; mixing data directories across instances; crash where vlogs lived on a failed disk but the LSM survived.

Related errors


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