Skip to main content

Delete a channel

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

Delete a single channel

Call deleteWithCompletion: on a channel instance. This method does not delete the messages within the channel — it only removes the channel from the SDK's channel list. 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 deleteChannels:completion: with an array of channel identifiers (recommended max: 20):

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 method to clear all channels at once. Retrieve the channel list first, then delete the channels by identifier:

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
let channels = page?.data ?? []
guard error == nil, !channels.isEmpty else { return }

let identifiers = channels.map { ChannelIdentifier(channelType: $0.channelType, channelId: $0.channelId) }
BaseChannel.deleteChannels(identifiers: identifiers) { deleteError in
print("Result: \(deleteError == nil ? "success" : deleteError!.localizedDescription)")
}
}