Delete messages

delete() either permanently removes a historical message from Message Persistence or marks it as deleted (if you remove the message with the soft option).

Requires Message Persistence configuration

To manage messages, you must enable Message Persistence for your app's keyset in the Admin Portal. To delete messages from PubNub storage, you must also mark the Enable Delete-From-History option.

Method signature

This method takes the following parameters:

message.delete(
soft: Bool = false,
preserveFiles: Bool = false,
completion: ((Swift.Result<MessageImpl?, Error>) -> Void)? = nil
)

Input

ParameterTypeRequiredDefaultDescription
softBoolNofalseDefine if you want to permanently remove message data. By default, the message data gets permanently deleted from Message Persistence. If you set this parameter to true, the Message object gets the deleted status and you can still restore/get its data.
preserveFilesBoolNofalseDefine if you want to keep the files attached to the message or remove them.

Output

TypeDescription
MessageImplFor hard delete, the method returns the last version of the MessageImpl object before it was permanently deleted. For soft delete, an updated message instance with an added deleted action type.

Basic usage

Permanently delete the message with the 16200000000000001 timetoken from the support channel.

/// Assuming you have a "chat" instance and "support" channel available

let timetoken: Timetoken = 16200000000000001

/// Fetch the channel metadata
chat?.getChannel(channelId: "support") { [weak self] in
switch $0 {
case let .success(channel):
if let channel = channel {
self?.channel = channel
/// Fetch the message with the specific timetoken on the "support" channel
channel.getMessage(timetoken: timetoken) {
switch $0 {
case let .success(message):
if let message = message {
show all 42 lines

Other examples

Archive (soft delete) the message with the 16200000000000001 timetoken from the support channel, keeping its data in Message Persistence.

/// Assuming you have a "chat" instance and "support" channel available

let timetoken: Timetoken = 16200000000000001

/// Fetch the channel metadata
chat?.getChannel(channelId: "support") { [weak self] in
switch $0 {
case let .success(channel):
if let channel = channel {
self?.channel = channel
/// Fetch the message with the specific timetoken on the "support" channel
channel.getMessage(timetoken: timetoken) {
switch $0 {
case let .success(message):
if let message = message {
show all 42 lines
Last updated on