docker/cli · error

failed to remove some resources from stack: {namespace}

Error message

failed to remove some resources from stack: {namespace}

What it means

Returned by runRemove (cli/command/stack/remove.go:85) as a per-stack aggregate when any of removeServices/removeSecrets/removeConfigs/removeNetworks reports a failure (their hasError bool is true). It signals partial removal — some resources were deleted, some were not — typically due to networks still in use or transient API errors.

Source

Thrown at cli/command/stack/remove.go:85

		configs, err := getStackConfigs(ctx, apiClient, namespace)
		if err != nil {
			return err
		}

		if len(services.Items)+len(networks.Items)+len(secrets.Items)+len(configs.Items) == 0 {
			_, _ = fmt.Fprintln(dockerCli.Err(), "Nothing found in stack:", namespace)
			continue
		}

		// TODO(thaJeztah): change this "hasError" boolean to return a (multi-)error for each of these functions instead.
		hasError := removeServices(ctx, dockerCli, services.Items)
		hasError = removeSecrets(ctx, dockerCli, secrets.Items) || hasError
		hasError = removeConfigs(ctx, dockerCli, configs.Items) || hasError
		hasError = removeNetworks(ctx, dockerCli, networks.Items) || hasError

		if hasError {
			errs = append(errs, errors.New("failed to remove some resources from stack: "+namespace))
			continue
		}

		if !opts.detach {
			err = waitOnTasks(ctx, apiClient, namespace)
			if err != nil {
				errs = append(errs, fmt.Errorf("failed to wait on tasks of stack: %s: %w", namespace, err))
			}
		}
	}
	return errors.Join(errs...)
}

func sortServiceByName(services []swarm.Service) func(i, j int) bool {
	return func(i, j int) bool {
		return services[i].Spec.Name < services[j].Spec.Name
	}
}

View on GitHub (pinned to 4f84911bfe)

Solutions

  1. Re-run `docker stack rm <stack>`; transient or dependency-ordered failures often clear once services drain.
  2. List and remove stubborn resources manually: `docker network rm <id>`, `docker service rm <id>`.
  3. Ensure no containers are still attached to stack networks before removal (stop tasks first).
  4. Check stderr above the error line — each remove* helper prints the specific per-resource failure.

Example fix

// before
docker stack rm mystack   # exits with: failed to remove some resources from stack: mystack

// after: read the per-resource stderr, then target leftovers
docker network rm mystack_default   # e.g. network still in use
docker stack rm mystack            # retry
Defensive patterns

Strategy: retry

Try / catch

// Retry stack removal with backoff; collect per-resource failures.
for attempt := 0; attempt < 3; attempt++ {
	err := runRemove(ctx, dockerCli, opts)
	if err == nil { break }
	if !isRetryableRemoveErr(err) { return err }
	time.Sleep(time.Duration(attempt+1) * time.Second)
}

Prevention

When it happens

Trigger: `docker stack rm <stack>` where at least one resource deletion fails: a network still has attached endpoints, a service/config was removed concurrently, or an API/RBAC error occurred mid-loop.

Common situations: A network still referenced by a lingering container or task; a race with concurrent cleanup; permission limits on the API; resources already gone between list and delete.

Related errors


AI-assisted analysis of docker/cli@4f84911bfe (2026-08-07). Data as JSON: /api/errors/abd0b9bdfad97953. Report an issue: GitHub.