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
) async throws -> MessageImpl?
Input
Parameter | Description |
---|---|
soft Type: Bool Default: false | Define 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. |
preserveFiles Type: Bool Default: false | Define if you want to keep the files attached to the message or remove them. |
Output
Parameter | Description |
---|---|
MessageImpl | For hard delete, the method returns nil . For soft delete, an updated message instance with an added deleted action type. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Permanently delete the message with the 16200000000000001
timetoken from the support
channel.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
if let message = try await channel.getMessage(timetoken: 16200000000000001) {
try await message.delete()
} else {
debugPrint("Message not found")
}
} else {
debugPrint("Channel not found")
}
}
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 reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
if let message = try await channel.getMessage(timetoken: 16200000000000001) {
try await message.delete(soft: true)
} else {
debugPrint("Message not found")
}
} else {
debugPrint("Channel not found")
}
}