grafana/k6 · warning
%s already exists. Use the `--force` flag to overwrite it
Error message
%s already exists. Use the `--force` flag to overwrite it
What it means
Returned by `k6 new` when the target script file (the positional arg, default 'script.js') already exists on the filesystem and the --force/-f flag was not provided. This is a deliberate guard against clobbering an existing script; the error names the target and tells you the exact escape hatch.
Source
Thrown at internal/cmd/new.go:44
flags.SortFlags = false
flags.BoolVarP(&c.overwriteFiles, "force", "f", false, "overwrite existing files")
flags.StringVar(&c.templateType, "template", "minimal", "template type (choices: minimal, protocol, browser) or relative/absolute path to a custom template file") //nolint:lll
flags.StringVar(&c.projectID, "project-id", "", "specify the Grafana Cloud project ID for the test")
return flags
}
func (c *newScriptCmd) run(_ *cobra.Command, args []string) (err error) {
target := defaultNewScriptName
if len(args) > 0 {
target = args[0]
}
fileExists, err := fsext.Exists(c.gs.FS, target)
if err != nil {
return err
}
if fileExists && !c.overwriteFiles {
return fmt.Errorf("%s already exists. Use the `--force` flag to overwrite it", target)
}
// Initialize template manager and validate template before creating any files
tm, err := templates.NewTemplateManager(c.gs.FS)
if err != nil {
return fmt.Errorf("error initializing template manager: %w", err)
}
tmpl, err := tm.GetTemplate(c.templateType)
if err != nil {
return fmt.Errorf("error retrieving template: %w", err)
}
// Prepare template arguments
argsStruct := templates.TemplateArgs{
ScriptName: target,
ProjectID: c.projectID,
}View on GitHub (pinned to 93accf6570)
Solutions
- Pass --force to overwrite: `k6 new script.js --force`
- Or choose a fresh target name: `k6 new load-test.js`
- Or move/back up the existing file first if you need its contents
Example fix
# before $ k6 new script.js # error: script.js already exists. Use the `--force` flag to overwrite it # after $ k6 new script.js --force
Defensive patterns
Strategy: validation
Validate before calling
# Idempotent scaffolding: decide overwrite policy up front target="script.js" if [ -e "$target" ] && [ "$K6_NEW_FORCE" != "1" ]; then echo "$target exists — skipping k6 new (set K6_NEW_FORCE=1 to overwrite)" else k6 new "$target" $([ "$K6_NEW_FORCE" = "1" ] && echo --force) fi
Prevention
- Check file existence in bootstrap scripts before calling k6 new
- Use --force only in throwaway/workspaces where overwrite is intended
- Keep generated scripts in git so accidental overwrites are recoverable
When it happens
Trigger: Running `k6 new script.js` twice in a row; running `k6 new` with no arg in a directory that already has script.js; generating into a path that a previous tool created.
Common situations: Re-running scaffolding after a failed first attempt; CI/bootstrap steps that assume a clean workspace; teammates scaffolding into an initialized repo.
Related errors
- error initializing template manager: %w
- error retrieving template: %w
- failed to execute template %s: %w
- failed to get absolute path for template %s: %w
- failed to read template file %s: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/84ba72139ad85818.
Report an issue: GitHub.