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
- Objective-C
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")")
}
}
NCDirectChannel *channel = [[NCDirectChannel alloc] initWithChannelId:@"targetUserId"];
[channel deleteWithCompletion:^(BOOL isSuccess, NCError * _Nullable error) {
if (isSuccess) {
NSLog(@"Channel deleted");
} else {
NSLog(@"Failed to delete: %@", 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
- Objective-C
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")
}
}
NCChannelIdentifier *identifier1 = [[NCChannelIdentifier alloc] initWithChannelType:NCChannelTypeDirect
channelId:@"user1"];
NCChannelIdentifier *identifier2 = [[NCChannelIdentifier alloc] initWithChannelType:NCChannelTypeDirect
channelId:@"user2"];
[NCBaseChannel deleteChannels:@[identifier1, identifier2]
completion:^(NCError * _Nullable error) {
if (error == nil) {
NSLog(@"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
- Objective-C
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)")
}
}
NCChannelsQueryParams *params = [[NCChannelsQueryParams alloc] init];
params.channelTypes = @[@(NCChannelTypeDirect), @(NCChannelTypeGroup), @(NCChannelTypeSystem)];
params.pageSize = 50;
NCChannelsQuery *query = [NCBaseChannel createChannelsQueryWithParams:params];
[query loadNextPageWithCompletion:^(NSArray<NCBaseChannel *> * _Nullable channels, NCError * _Nullable error) {
if (error == nil && channels.count > 0) {
NSMutableArray<NCChannelIdentifier *> *identifiers = [NSMutableArray array];
for (NCBaseChannel *ch in channels) {
NCChannelIdentifier *identifier = [[NCChannelIdentifier alloc] initWithChannelType:ch.channelType
channelId:ch.channelId];
[identifiers addObject:identifier];
}
[NCBaseChannel deleteChannels:identifiers completion:^(NCError * _Nullable deleteError) {
NSLog(@"Result: %@", deleteError == nil ? @"success" : deleteError.localizedDescription);
}];
}
}];