cloudflare/cloudflared · error

agent service %s is not installed, so it could not be uninst

Error message

agent service %s is not installed, so it could not be uninstalled

What it means

Returned by uninstallWindowsService when the Windows Service Control Manager connection succeeds but OpenService cannot find the cloudflared agent service. It means there is nothing installed under the expected service name, so the uninstall request cannot proceed.

Source

Thrown at cmd/cloudflared/windows_service.go:393

	log.Info().Msg("Agent service for cloudflared installed successfully")
	return nil
}

func uninstallWindowsService(c *cli.Context) error {
	log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog).
		With().
		Str(LogFieldWindowsServiceName, windowsServiceName).Logger()

	log.Info().Msg("Uninstalling cloudflared agent service")
	m, err := mgr.Connect()
	if err != nil {
		return errors.Wrap(err, "Cannot establish a connection to the service control manager")
	}
	defer m.Disconnect()
	s, err := m.OpenService(windowsServiceName)
	if err != nil {
		return fmt.Errorf("agent service %s is not installed, so it could not be uninstalled", windowsServiceName)
	}
	defer s.Close()

	if status, err := s.Query(); err == nil && status.State == svc.Running {
		log.Info().Msg("Stopping cloudflared agent service")
		if _, err := s.Control(svc.Stop); err != nil {
			log.Info().Err(err).Msg("Failed to stop cloudflared agent service, you may need to stop it manually to complete uninstall.")
		}
	}

	err = s.Delete()
	if err != nil {
		return errors.Wrap(err, "Cannot delete agent service")
	}
	log.Info().Msg("Agent service for cloudflared was uninstalled successfully")
	err = eventlog.Remove(windowsServiceName)
	if err != nil {
		return errors.Wrap(err, "Cannot remove event logger")

View on GitHub (pinned to 2253eeeb25)

Solutions

  1. Verify the service exists with `sc query cloudflared` and match the exact service name before uninstalling.
  2. If the goal is idempotent teardown, treat a missing service as success and skip the uninstall step.
  3. If the service was installed under a different name, uninstall using the binary that installed it or `sc delete <name>`.

Example fix

// before: blind uninstall fails when absent
cloudflared service uninstall
// after: idempotent guard in a script
sc query cloudflared >nul 2>&1 && cloudflared service uninstall || echo service not installed, skipping
Defensive patterns

Strategy: validation

Validate before calling

// Go: check service existence before uninstalling
m, err := mgr.Connect()
if err != nil { return err }
s, err := m.OpenService(windowsServiceName)
if err != nil {
    // treat as already-uninstalled
    return nil
}
s.Close()

Try / catch

if err := uninstallWindowsService(); err != nil {
    if strings.Contains(err.Error(), "is not installed") { return nil /* idempotent */ }
    return err
}

Prevention

When it happens

Trigger: Running `cloudflared service uninstall` (uninstallWindowsService) when the service was never installed, was already uninstalled, or was installed under a different service name (windowsServiceName mismatch).

Common situations: Double-uninstall in a teardown script; installing via a different binary/name (e.g. legacy service name) and trying to remove it with the new name; uninstalling on a machine where the install step failed earlier.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of cloudflare/cloudflared@2253eeeb25 (2026-09-06). Data as JSON: /api/errors/d81b29d33b728908. Report an issue: GitHub.