{"record":{"id":"1b5d86741ad3c401","repo":"siyuan-note/siyuan","slug":"template-already-exists-use-overwrite-to-replac","errorCode":null,"errorMessage":"template already exists, use --overwrite to replace: %s","messagePattern":"template already exists, use --overwrite to replace: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/cli/cmd/template.go","lineNumber":164,"sourceCode":"\t\tid, _ := cmd.Flags().GetString(\"id\")\n\t\tname, _ := cmd.Flags().GetString(\"name\")\n\t\tif id == \"\" {\n\t\t\treturn fmt.Errorf(\"--id is required\")\n\t\t}\n\t\tif name == \"\" {\n\t\t\treturn fmt.Errorf(\"--name is required\")\n\t\t}\n\t\toverwrite, _ := cmd.Flags().GetBool(\"overwrite\")\n\t\tif dryRun {\n\t\t\tfmt.Printf(\"[dry-run] Would save document %s as template \\\"%s\\\"\\n\", id, name)\n\t\t\treturn nil\n\t\t}\n\t\tcode, err := model.DocSaveAsTemplate(id, name, overwrite)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif code == 1 {\n\t\t\treturn fmt.Errorf(\"template already exists, use --overwrite to replace: %s\", name)\n\t\t}\n\t\tfmt.Printf(\"%s.md\\n\", name)\n\t\treturn nil\n\t},\n}\n\nvar templateCreateCmd = &cobra.Command{\n\tUse:   \"create --name <name> [--data <markdown> | --file <path>]\",\n\tShort: \"Create a template from markdown content\",\n\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\tname, _ := cmd.Flags().GetString(\"name\")\n\t\tif name == \"\" {\n\t\t\treturn fmt.Errorf(\"--name is required\")\n\t\t}\n\t\tcontent, err := resolveData(cmd)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/cli/cmd/template.go#L146-L182","documentation":"`model.DocSaveAsTemplate` returns code 1 when a template file with the requested name already exists in the templates folder and `--overwrite` was not set. The CLI converts that into this error to protect an existing template from silent replacement.","triggerScenarios":"Running `siyuan template save-as --id <id> --name <existing-template>` without `--overwrite`; the templates directory already contains `<name>.md`.","commonSituations":"Re-running an idempotent script a second time; name collision with a template created earlier or synced from another workspace; re-exporting a doc after renaming it to an existing template name.","solutions":["Add `--overwrite` to intentionally replace: `siyuan template save-as --id <id> --name mytpl --overwrite`","Choose a different `--name`","Inspect the templates folder (data/templates/<name>.md) to confirm the existing template can be replaced or back it up first"],"exampleFix":"// before\nsiyuan template save-as --id 20240101120000-abcdefg --name mytpl\n// after\nsiyuan template save-as --id 20240101120000-abcdefg --name mytpl --overwrite","handlingStrategy":"validation","validationCode":"if [ -f \"data/templates/$NAME.md\" ]; then\n  EXTRA=\"--overwrite\"\nfi\nsiyuan template save-as --id \"$DOC_ID\" --name \"$NAME\" $EXTRA","typeGuard":null,"tryCatchPattern":"if ! siyuan template save-as --id \"$ID\" --name \"$NAME\"; then\n  echo \"template exists; retry with --overwrite or pick a new name\" >&2\nfi","preventionTips":["Check data/templates/<name>.md existence before saving","Use unique names (e.g. include a date suffix) in scripts","Back up existing templates before using --overwrite"],"tags":["cli","template","collision"],"backgroundTag":"file-already-exists","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}