Skip to main content

Delete a channel

To remove one or more channels from the local channel list, use the SDK's channel deletion APIs. The client-side channel list is generated from local messages, so deleting a channel removes the local channel record only.

Delete a single channel

Call delete(completion:) on a channel instance. This method does not delete messages in the channel. After deletion, refresh the UI to hide the channel.

swift
import NexconnChatSDK

guard let channel = DirectChannel(channelId: "targetUserId") else {
return
}

channel.delete { isSuccess, error in
if isSuccess {
print("Channel deleted")
} else {
print("Failed to delete: \(error?.localizedDescription ?? "unknown error")")
}
}

If a new message arrives for a deleted channel, the channel reappears in the list with full message history.

The SDK does not provide a single method to delete both a channel and its messages. To also delete messages, call the message deletion API separately. See Delete messages.

Delete multiple channels by identifier

Call BaseChannel.deleteChannels(identifiers:completion:) with an array of channel identifiers. The recommended maximum is 20 identifiers per request.

swift
import NexconnChatSDK

let identifier1 = ChannelIdentifier(channelType: .direct, channelId: "user1")
let identifier2 = ChannelIdentifier(channelType: .direct, channelId: "user2")

BaseChannel.deleteChannels(identifiers: [identifier1, identifier2]) { error in
if error == nil {
print("Delete succeeded")
}
}

Delete all channels

The SDK does not provide a one-call method to clear all channels. Query the local channel list first, then delete the returned channel identifiers:

swift
import NexconnChatSDK

let params = ChannelsQueryParams()
params.setChannelTypes([.direct, .group, .system])
params.pageSize = 50

let query = BaseChannel.createChannelsQuery(params: params)
query.loadNextPage { page, error in
guard let channels = page?.data, error == nil, !channels.isEmpty else {
return
}

let identifiers = channels.map {
ChannelIdentifier(channelType: $0.channelType, channelId: $0.channelId)
}

BaseChannel.deleteChannels(identifiers: identifiers) { deleteError in
print(deleteError == nil ? "success" : deleteError!.localizedDescription)
}
}