{"record":{"id":"f1e81c627831475a","repo":"benbjohnson/litestream","slug":"cannot-restore-output-path-already-exists-and-is","errorCode":null,"errorMessage":"cannot restore, output path already exists and is not empty: %s. Use -force to overwrite","messagePattern":"cannot restore, output path already exists and is not empty: (.+?)\\. Use -force to overwrite","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/litestream/restore.go","lineNumber":280,"sourceCode":"\tif err != nil || len(infos) == 0 {\n\t\treturn \"\"\n\t}\n\treturn infos[len(infos)-1].MaxTXID.String()\n}\n\nfunc (c *RestoreCommand) prepareOutputPath(path string, force bool) error {\n\tinfo, err := os.Stat(path)\n\tif os.IsNotExist(err) {\n\t\treturn nil\n\t} else if err != nil {\n\t\treturn fmt.Errorf(\"cannot access output path: %w\", err)\n\t}\n\tif info.IsDir() {\n\t\treturn fmt.Errorf(\"cannot restore, output path is a directory: %s\", path)\n\t}\n\n\tif info.Size() > 0 && !force {\n\t\treturn fmt.Errorf(\"cannot restore, output path already exists and is not empty: %s. Use -force to overwrite\", path)\n\t}\n\n\tfor _, sidecarPath := range []string{path + \"-wal\", path + \"-shm\", path + \"-journal\"} {\n\t\tif _, err := os.Stat(sidecarPath); err == nil && !force {\n\t\t\treturn fmt.Errorf(\"cannot restore, SQLite sidecar path already exists: %s. Use -force to overwrite\", sidecarPath)\n\t\t} else if err != nil && !os.IsNotExist(err) {\n\t\t\treturn fmt.Errorf(\"cannot access SQLite sidecar path: %w\", err)\n\t\t}\n\t}\n\n\tfor _, removePath := range []string{path, path + \"-wal\", path + \"-shm\", path + \"-journal\"} {\n\t\tif err := os.Remove(removePath); err != nil && !os.IsNotExist(err) {\n\t\t\treturn fmt.Errorf(\"remove existing output path: %w\", err)\n\t\t}\n\t}\n\treturn nil\n}\n","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/benbjohnson/litestream/blob/4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3/cmd/litestream/restore.go#L262-L298","documentation":"prepareOutputPath refuses to overwrite an existing non-empty file at the output path unless -force is given. os.Stat found a regular file whose size is greater than zero and the force flag was false, so restore aborts to avoid silently destroying existing data.","triggerScenarios":"Running `litestream restore -o <path>` where <path> exists as a non-empty regular file and no -force flag is passed.","commonSituations":"Re-running a restore into a location that already holds a database or leftover file; a partially written file from a previous failed run; CI reusing a workspace without cleaning artifacts; restoring over the live database path of a running application.","solutions":["Re-run the restore with -force to overwrite the existing file","Delete or move the existing file, then re-run the restore","Choose a different -o output path","Verify the existing file isn't needed (e.g. it's the live DB of a running app) before forcing"],"exampleFix":"// before\nlitestream restore -o /data/app.db /path/to/db\ncannot restore, output path already exists and is not empty: /data/app.db. Use -force to overwrite\n// after\nlitestream restore -force -o /data/app.db /path/to/db","handlingStrategy":"validation","validationCode":"if info, err := os.Stat(outputPath); err == nil && info.Size() > 0 && !forceFlag {\n    return fmt.Errorf(\"%s exists; pass -force to overwrite\", outputPath)\n}","typeGuard":null,"tryCatchPattern":"if err := cmd.Run(ctx); err != nil {\n    if strings.Contains(err.Error(), \"already exists and is not empty\") {\n        // decide: add -force or pick a new output path\n    }\n    return err\n}","preventionTips":["Clean old artifacts out of the restore target before scripted restores","Only pass -force when you have verified the existing file is disposable","Never restore over the live database of a running application","In CI, use a fresh workspace or explicitly rm the target first"],"tags":["filesystem","overwrite","restore"],"backgroundTag":"file-already-exists","analyzedSha":"4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3","analyzedAt":"2026-09-06T18:29:25.564Z","contentChangedAt":"2026-09-06T18:29:25.564Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}