Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions internal/cmd/service/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package delete

import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/zeabur/cli/internal/cmdutil"
"github.com/zeabur/cli/internal/util"
"github.com/zeabur/cli/pkg/fill"
)

type Options struct {
id string
name string

environmentID string

skipConfirm bool
}

func NewCmdDelete(f *cmdutil.Factory) *cobra.Command {
opts := &Options{}
zctx := f.Config.GetContext()

cmd := &cobra.Command{
Use: "delete",
Short: "Delete a service",
PreRunE: util.RunEChain(
util.NeedProjectContextWhenNonInteractive(f),
util.DefaultIDNameByContext(zctx.GetService(), &opts.id, &opts.name),
util.DefaultIDByContext(zctx.GetEnvironment(), &opts.environmentID),
),
RunE: func(cmd *cobra.Command, args []string) error {
return runDelete(f, opts)
},
}

util.AddServiceParam(cmd, &opts.id, &opts.name)
util.AddEnvOfServiceParam(cmd, &opts.environmentID)
cmd.Flags().BoolVarP(&opts.skipConfirm, "yes", "y", false, "Skip confirmation")

return cmd
}

func runDelete(f *cmdutil.Factory, opts *Options) error {
if f.Interactive {
return runDeleteInteractive(f, opts)
}
return runDeleteNonInteractive(f, opts)
}

func runDeleteInteractive(f *cmdutil.Factory, opts *Options) error {
zctx := f.Config.GetContext()

if _, err := f.ParamFiller.ServiceByNameWithEnvironment(fill.ServiceByNameWithEnvironmentOptions{
ProjectCtx: zctx,
ServiceID: &opts.id,
ServiceName: &opts.name,
EnvironmentID: &opts.environmentID,
CreateNew: false,
}); err != nil {
return err
}

return runDeleteNonInteractive(f, opts)
}

func runDeleteNonInteractive(f *cmdutil.Factory, opts *Options) error {
if err := checkParams(opts); err != nil {
return err
}

// if name is set, get service id by name
if opts.id == "" && opts.name != "" {
service, err := util.GetServiceByName(f.Config, f.ApiClient, opts.name)
if err != nil {
return err
}
opts.id = service.ID
}

// to show friendly message
idOrName := opts.name
if idOrName == "" {
idOrName = opts.id
}

// double check
if f.Interactive && !opts.skipConfirm {
confirm, err := f.Prompter.Confirm(fmt.Sprintf("Are you sure to delete service <%s>?", idOrName), false)
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confirmation prompt text is a bit awkward ("Are you sure to delete..."). Consider rephrasing to something like "Are you sure you want to delete service <...>?" for clarity.

Suggested change
confirm, err := f.Prompter.Confirm(fmt.Sprintf("Are you sure to delete service <%s>?", idOrName), false)
confirm, err := f.Prompter.Confirm(fmt.Sprintf("Are you sure you want to delete service <%s>?", idOrName), false)

Copilot uses AI. Check for mistakes.
if err != nil {
return err
}
if !confirm {
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the user declines the confirmation prompt, the command returns without any message. Consider logging a clear cancellation message (consistent with other delete commands) so users understand no action was taken.

Suggested change
if !confirm {
if !confirm {
f.Log.Info("Service deletion cancelled")

Copilot uses AI. Check for mistakes.
return nil
}
} else if !f.Interactive && !opts.skipConfirm {
f.Log.Info("Please use --yes to confirm deletion without interactive prompt")
return nil
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In non-interactive mode, missing --yes currently logs an info message and returns nil, which exits with status 0. For scripting/CI safety, consider returning a non-nil error here so callers can reliably detect that the delete was refused (while still printing the guidance to use --yes).

Suggested change
return nil
return fmt.Errorf("deletion not confirmed: rerun with --yes to confirm deletion in non-interactive mode")

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping return nil to stay consistent with the existing project delete command, which uses the same pattern. Changing the exit behavior for just this command would introduce inconsistency.

}
Comment on lines +98 to +101
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Return a non-zero exit when --yes is required but missing.

Right now the command logs a message and exits successfully even though nothing happened. That’s risky for scripts that assume success means deletion.

💡 Proposed fix
-	} else if !f.Interactive && !opts.skipConfirm {
-		f.Log.Info("Please use --yes to confirm deletion without interactive prompt")
-		return nil
-	}
+	} else if !f.Interactive && !opts.skipConfirm {
+		return fmt.Errorf("confirmation required: re-run with --yes to delete non-interactively")
+	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} else if !f.Interactive && !opts.skipConfirm {
f.Log.Info("Please use --yes to confirm deletion without interactive prompt")
return nil
}
} else if !f.Interactive && !opts.skipConfirm {
return fmt.Errorf("confirmation required: re-run with --yes to delete non-interactively")
}
🤖 Prompt for AI Agents
In `@internal/cmd/service/delete/delete.go` around lines 98 - 101, The branch that
currently logs "Please use --yes to confirm deletion without interactive prompt"
should return a non-nil error to produce a non-zero exit code instead of
returning nil; update the conditional using f.Interactive and opts.skipConfirm
so it returns an error (e.g., a formatted error or cli.Exit) after logging via
f.Log.Info (or replace the log with f.Log.Error) with a clear message like "use
--yes to confirm deletion without interactive prompt" so scripts detect failure.
Ensure you modify the function that contains the f.Interactive /
opts.skipConfirm check to propagate the error return type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping return nil to stay consistent with project delete, which uses the same pattern (internal/cmd/project/delete/delete.go:61).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check the project delete command to verify the pattern
cat internal/cmd/project/delete/delete.go | head -70 | tail -20

Length of output: 471


@canyugs Confirmed! I can see the same pattern in project delete where it returns nil with an info message when --yes is missing. Consistency across commands makes sense. Thanks for the context! 👍


err := f.ApiClient.DeleteService(context.Background(), opts.id, opts.environmentID)
if err != nil {
return fmt.Errorf("delete service failed: %w", err)
}

f.Log.Infof("Service <%s> deleted successfully", idOrName)

return nil
}

func checkParams(opts *Options) error {
if opts.id == "" && opts.name == "" {
return fmt.Errorf("--id or --name is required")
}

if opts.environmentID == "" {
return fmt.Errorf("--env-id is required")
}

return nil
}
2 changes: 2 additions & 0 deletions internal/cmd/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package service
import (
"github.com/spf13/cobra"

serviceDeleteCmd "github.com/zeabur/cli/internal/cmd/service/delete"
serviceDeployCmd "github.com/zeabur/cli/internal/cmd/service/deploy"
serviceExposeCmd "github.com/zeabur/cli/internal/cmd/service/expose"
serviceGetCmd "github.com/zeabur/cli/internal/cmd/service/get"
Expand Down Expand Up @@ -33,6 +34,7 @@ func NewCmdService(f *cmdutil.Factory) *cobra.Command {
cmd.AddCommand(serviceRestartCmd.NewCmdRestart(f))
cmd.AddCommand(serviceRedeployCmd.NewCmdRedeploy(f))
cmd.AddCommand(serviceSuspendCmd.NewCmdSuspend(f))
cmd.AddCommand(serviceDeleteCmd.NewCmdDelete(f))
cmd.AddCommand(serviceDeployCmd.NewCmdDeploy(f))
cmd.AddCommand(serviceInstructionCmd.NewCmdInstruction(f))
cmd.AddCommand(serviceNetworkCmd.NewCmdPrivateNetwork(f))
Expand Down
1 change: 1 addition & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type (
UploadZipToService(ctx context.Context, projectID string, serviceID string, environmentID string, zipBytes []byte) (*model.Service, error)
GetDNSName(ctx context.Context, serviceID string) (string, error)
UpdateImageTag(ctx context.Context, serviceID string, environmentID string, tag string) error
DeleteService(ctx context.Context, id string, environmentID string) error
}

VariableAPI interface {
Expand Down
13 changes: 13 additions & 0 deletions pkg/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,16 @@ func (c *client) UpdateImageTag(ctx context.Context, serviceID, environmentID, t

return nil
}

func (c *client) DeleteService(ctx context.Context, id string, environmentID string) error {
var mutation struct {
DeleteService bool `graphql:"deleteService(_id: $id, environmentID: $environmentID)"`
}

err := c.Mutate(ctx, &mutation, V{
"id": ObjectID(id),
"environmentID": ObjectID(environmentID),
})

return err
}
Loading