{"record":{"id":"b72c5d997e86931f","repo":"ory/kratos","slug":"expected-to-get-the-dsn-as-an-argument-or-the-re","errorCode":null,"errorMessage":"expected to get the DSN as an argument, or the \"read-from-env\" flag","messagePattern":"expected to get the DSN as an argument, or the \"read-from-env\" flag","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cleanup/handler.go","lineNumber":34,"sourceCode":"\t\"github.com/ory/kratos/driver/config\"\n\t\"github.com/ory/x/flagx\"\n)\n\ntype CleanupHandler struct{}\n\nfunc NewCleanupHandler() *CleanupHandler {\n\treturn &CleanupHandler{}\n}\n\nfunc (h *CleanupHandler) CleanupSQL(cmd *cobra.Command, args []string) error {\n\topts := []configx.OptionModifier{\n\t\tconfigx.WithFlags(cmd.Flags()),\n\t\tconfigx.SkipValidation(),\n\t}\n\n\tif !flagx.MustGetBool(cmd, \"read-from-env\") {\n\t\tif len(args) != 1 {\n\t\t\treturn errors.New(`expected to get the DSN as an argument, or the \"read-from-env\" flag`)\n\t\t}\n\t\topts = append(opts, configx.WithValue(config.ViperKeyDSN, args[0]))\n\t}\n\n\td, err := driver.NewWithoutInit(\n\t\tcmd.Context(),\n\t\tcmd.ErrOrStderr(),\n\t\tdriver.WithConfigOptions(opts...),\n\t)\n\tif len(d.Config().DSN(cmd.Context())) == 0 {\n\t\treturn errors.New(`required config value \"dsn\" was not set`)\n\t} else if err != nil {\n\t\treturn errors.Wrap(err, \"An error occurred initializing cleanup\")\n\t}\n\n\terr = d.Init(cmd.Context(), &contextx.Default{})\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"An error occurred initializing cleanup\")","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/ory/kratos/blob/b86338da04a040247a07f46100a86dcfb3875909/cmd/cleanup/handler.go#L16-L52","documentation":"CleanupSQL (the `hydra/cleanup sql` / Ory `cleanup sql` command) requires a database DSN. It either takes the DSN as a positional CLI argument or reads it from the environment when the `read-from-env` flag is set. This error is thrown when neither is supplied: no argument was passed and `--read-from-env` was not used.","triggerScenarios":"Running the cleanup SQL command with no positional argument while `read-from-env` is false (or absent). E.g. `cleanup sql` alone, or passing zero args when the cobra command expects exactly one.","commonSituations":"Forgetting the DSN argument when scripting the cleanup command; running the binary manually instead of via a Helm/K8s job that normally injects the DSN; setting a DSN env var but forgetting the `--read-from-env` flag, so the env value is ignored.","solutions":["Pass the DSN as the single positional argument: `cleanup sql postgres://user:pass@host:5432/db?sslmode=disable`","Or add the `--read-from-env` flag so the DSN is read from the environment (DSN env var)","Ensure exactly one argument is given — 0 or more than 1 args both trigger this error","Check your job/deployment manifest that the command and args are wired correctly"],"exampleFix":"// before\ncleanup sql\n// after\ncleanup sql --read-from-env\n// or\ncleanup sql postgres://user:pass@host:5432/db?sslmode=disable","handlingStrategy":"validation","validationCode":"if len(os.Args) < 2 && !readFromEnv {\n  log.Fatal(`expected the DSN as an argument or the \"read-from-env\" flag`)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always wrap the cleanup command in a wrapper that either passes the DSN or sets --read-from-env","In Kubernetes jobs, set --read-from-env and inject the DSN via env from a secret"],"tags":["cli","config","database"],"backgroundTag":"missing-required-argument","analyzedSha":"b86338da04a040247a07f46100a86dcfb3875909","analyzedAt":"2026-09-07T15:58:15.934Z","contentChangedAt":"2026-09-07T15:58:15.934Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}