Deleting messages
Since PubNub stores all messages in a time-series database, it doesn’t expose an API to directly update a message. However, clients can update messages (or soft delete them) by passing another version of the message using the Message Reactions feature.
Soft delete a message
Use the addMessageAction
method to soft delete a message after it has been published by adding an action to it.
Soft deleting a message just attaches a delete flag to the message. Refer to hard delete if you want to permanently delete the message from Message Persistence for compliance reasons.
User ID / UUID
User ID is also referred to as UUID
/uuid
in some APIs and server responses but holds the value of the userId
parameter you set during initialization.
- JavaScript
- Swift
- Java
- Unity
pubnub.addMessageAction(
{
channel: 'ch-1'
messageTimetoken: '15610547826970040',
action: {
type: 'deleted',
value: '.',
},
},
function(status, response) {
}
);
extension MyAppMessageAction: MessageAction {}
let action = MyAppMessageAction(type: "deleted", value: ".")
pubnub.addMessageAction(
channel: "ch-1",
message: action,
messageTimetoken: 15610547826970040
) { result in
switch result {
case let .success(response):
print("Successfully Message Action Add Response: \(response)")
case let .failure(error):
print("Error from failed response: \(error.localizedDescription)")
}
show all 16 linespubnub.addMessageAction()
.channel("ch-1")
.messageAction(new PNMessageAction()
.setType("deleted")
.setValue(".")
.setMessageTimetoken(15610547826970040L)
)
.async(result -> { /* check result */ });
MessageActionAdd messageAct = new MessageActionAdd();
messageAct.ActionType = "deleted";
messageAct.ActionValue = ".";
pubnub.AddMessageActions().Channel("ch-1").MessageAction(messageAct).MessageTimetoken(15610547826970040).Async((result, status) => {
if (!status.Error) {
Debug.Log("result.ActionTimetoken: " + result.ActionTimetoken);
Debug.Log("result.ActionType: " + result.ActionType);
Debug.Log("result.ActionValue: " + result.ActionValue);
Debug.Log("result.MessageTimetoken: " + result.MessageTimetoken);
Debug.Log("result.UserID: " + result.UserId);
} else {
Debug.Log(status.Error);
Debug.Log(status.ErrorData.Info);
}
show all 16 linesPubNub triggers events when messages are soft deleted using message actions. Users subscribed to the channel can receive these events and mark the message as deleted.
{
"channel":"main",
"subscription":null,
"timetoken":"15610547826970040",
"publisher":"user-1",
"message":{
"source":"actions",
"version":"1.0",
"action":"added",
"data":{
"type":"deleted",
"value":".",
"messageTimetoken":"15610547826970040",
"actionTimetoken":"15610547826999081"
}
show all 17 linesHard delete a message
The deleteMessages
method permanently deletes messages from Message Persistence in a given channel. You can delete a single message, or a range of messages.
The Delete Message API looks backwards in time. The start timetoken parameter should be more forward in time than the end parameter: end < start.
In the example below, the start
and end
timetoken parameter values are 1/10 nanosecond (last digit of timetoken) apart to delete the message stored at the end
parameter's timetoken value.
Required setting in Admin Portal
You need to turn on the Enable Delete-From-History setting on your keyset in Admin Portal in order to hard delete messages.
We do not recommend heavy use of this API for performance reasons. Messages should be soft deleted when possible.
- JavaScript
- Swift
- Objective-C
- Java
- C#
- Python
pubnub.deleteMessages(
{
channel: 'chats.room1',
start: "15526611838554310",
end: "15526611838554309",
},
function (status, response) {
console.log(status, response);
}
);
pubnub.deleteMessageHistory(
from: "chats.room1",
start: 15526611838554310,
end: 15526611838554309
) { result in
switch result {
case let .success(response):
print("Successful Message Deletion Response: \(response)")
case let .failure(error):
print("Failed Message Deletion Response: \(error.localizedDescription)")
}
}
[self.client deleteMessagesFromChannel:@"chats.room1"
start:@15526611838554310 end:@15526611838554309
withCompletion:^(PNAcknowledgmentStatus *status) {
if (status.isError) {
// handle error condtion
}
else {
// confirm delete success
}
}];
pubnub.deleteMessages()
.channels(Arrays.asList("chats.room1"))
.start(15526611838554310L)
.end(15526611838554309L)
.async(result -> { /* check result */ });
pubnub.DeleteMessages().Channel("chats.room1")
.Start(15526611838554310)
.End(15526611838554309)
.Execute(new PNDeleteMessageResultExt((result, status) => {
if (status != null && status.Error) {
Console.WriteLine(status.ErrorData.Information);
}
else if (result != null) {
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(result));
}
}
));
envelope = PubNub(pnconf).delete_messages().channel('chats.room1') \
.start(15526611838554310) \
.end(15526611838554309) \
.sync()