---
title: End Chat
slug: sdk/ios-sdk-end-chat
createdAt: 2026-11-04T11:40:26.349Z
---

## Disconnect the Chat

*clears the context as well*

```swift
KindlySDK.endChat()
```

**Important:** The `endChat()` method now **preserves the SDK initialization state**. This means:

- ✅ The SDK remains initialized and ready for use
- ✅ You can call `displayChat()` again without needing to call `start()`
- ✅ Bot key, authentication, and configuration are preserved
- ✅ Only the active chat session and context are cleared

If you need to **completely terminate the SDK**, use [`kill()`](https://docs.kindly.ai/sdk/ios-sdk-kill-sdk) instead.

## Bot Switching Workflows

When switching between different bots, you have two workflow options:

### Option 1: Automatic Switching (Recommended)

The SDK automatically handles cleanup when you switch bot keys:

```swift
// Current bot
KindlySDK.start(botKey: "bot-1", market: "YOUR_MARKET")
KindlySDK.displayChat()

// Switch to different bot - no explicit endChat() needed
KindlySDK.start(botKey: "bot-2", market: "YOUR_MARKET")  // SDK handles cleanup internally  
KindlySDK.displayChat()
```

### Option 2: Explicit End-then-Start

For explicit control over the chat lifecycle:

```swift
// End current chat first
KindlySDK.endChat().then(on: DispatchQueue.main) { _ in
    // Start with new bot after cleanup completes
    KindlySDK.start(botKey: "new-bot-key", market: "YOUR_MARKET")
    KindlySDK.displayChat()
}
```

**Both approaches work identically** - the SDK ensures proper cleanup and reinitialization in both cases.

## When to Use endChat()

Use `endChat()` explicitly when you want to:

- **Disconnect completely**: Stop the chat service without switching to another bot
- **Clear session data**: Ensure all messages and context are cleared
- **Explicit lifecycle control**: Have fine-grained control over connection timing

The `endChat()` method returns a Promise that resolves when cleanup is complete, allowing you to chain subsequent operations safely.
