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: Boolean = false, preserveFiles: Boolean = false): PNFuture<Message?>

Input

ParameterTypeRequiredDefaultDescription
softBooleanNofalseDefine 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.
preserveFilesBooleanNofalseDefine if you want to keep the files attached to the message or remove them.

Output

TypeDescription
PNFuture<Message?>For hard delete, the method returns the last version of the Message 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.

val channel: Channel
// ...

// define the timetoken of the message to archive (soft delete)
val messageTimetoken: Long = 16200000000000001

channel.getMessage(messageTimetoken).async { messageResult ->
messageResult.onSuccess { message ->
// handle success
message?.delete()?.async { deleteResult ->
deleteResult.onSuccess {
// handle success
println("Message successfully deleted: ${it?.timetoken}")
}.onFailure {
// handle failure
show all 23 lines

Other examples

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

val channel: Channel
// ...

// define the timetoken of the message to archive (soft delete)
val messageTimetoken: Long = 16200000000000001

channel.getMessage(messageTimetoken).async { messageResult ->
messageResult.onSuccess { message ->
// handle success
message?.delete(soft = true)?.async { deleteResult ->
deleteResult.onSuccess {
// handle success
println("Message successfully soft deleted: ${it?.timetoken}")
}.onFailure {
// handle failure
show all 23 lines
Last updated on