Delete messages from history
This guide shows you how to delete messages from Message Persistence storage. You can remove all messages between two timetokens or target a single message by its publish timetoken.
Three things must be in place before you call the delete API:
- Delete-From-History enabled. Open the Admin Portal, go to your keyset settings, and turn on Enable Delete-From-History. The delete API returns an error if this toggle is off.
- Secret key. The PubNub client making the delete call must be initialized with your secret key. Use it only in server-side code. Never expose the secret key to client devices. For initialization steps, see Application setup.
deletepermission (if Access Manager is on). If Access Manager is enabled on your keyset, the token used for the request must grantdeleteon the target channel. See Access control.
No undo
Deletion is permanent. There is no recovery path after a successful call, so verify the timetoken range before you send the request.
Delete a range of messages
Pass start and end timetokens to remove all messages between two points in time. The call deletes the message at end but not the message at start, whichever of the two values is larger. For example, if a channel has messages at timetokens T - 1, T, and T + 1:
start | end | Deleted | Kept |
|---|---|---|---|
T - 1 | T + 1 | T, T + 1 | T - 1 |
T + 1 | T - 1 | T - 1, T | T + 1 |
- JavaScript
- Python
- Swift
- Objective-C
- Java
- C#
- Kotlin
- Dart
1
1envelope = PubNub(pnconf).delete_messages() \
2 .channel("my-ch") \
3 .start(123) \
4 .end(456) \
5 .sync()
1let startDate = NSNumber(value: (15101397027611671 as CUnsignedLongLong))
2let endDate = NSNumber(value: (15101397427611671 as CUnsignedLongLong))
3self.client.deleteMessagesFromChannel("channel", start: startDate, end: endDate, withCompletion: { (status) in
4
5 if !status.isError {
6 // Messages within specified time frame has been removed.
7 } else {
8 /**
9 * Handle message history download error. Check 'category' property to find out possible
10 * issue because of which request did fail.
11 *
12 * Request can be resent using: status.retry()
13 */
14 }
15})
1[self.client deleteMessagesFromChannel:@"channel" start:@15101397027611671 end:@15101397427611671
2 withCompletion:^(PNAcknowledgmentStatus *status) {
3
4 if (!status.isError) {
5 // Messages within specified time frame has been removed.
6 } else {
7 /**
8 * Handle message history download error. Check 'category' property to find out possible
9 * issue because of which request did fail.
10 *
11 * Request can be resent using: [status retry];
12 */
13 }
14}];
1
1
1
1await pubnub
2 .channel('channel-name')
3 .messages(
4 from: Timetoken(BigInt.parse('123345')),
5 to: Timetoken(BigInt.parse('123538293')),
6 )
7 .delete();
Delete a specific message
To target a single message, set end to its publish timetoken and start to that timetoken minus 1. PubNub returns the publish timetoken in the publish response. With messages at T - 1, T, and T + 1, this deletes only T. Setting start to the timetoken plus 1 deletes the same message.
For example, if the publish timetoken is 15526611838554310, pass 15526611838554309 as start and 15526611838554310 as end.
- JavaScript
- Python
- Swift
- Objective-C
- Java
- C#
- Kotlin
1
1envelope = PubNub(pnconf).delete_messages() \
2 .channel("my-ch") \
3 .start(15526611838554309) \
4 .end(15526611838554310) \
5 .sync()
1let startDate = NSNumber(value: (15526611838554309 as CUnsignedLongLong))
2let endDate = NSNumber(value: (15526611838554310 as CUnsignedLongLong))
3self.client.deleteMessagesFromChannel("channel", start: startDate, end: endDate, withCompletion: { (status) in
4
5 if !status.isError {
6 // Messages within specified time frame has been removed.
7 } else {
8 /**
9 * Handle message history download error. Check 'category' property to find out possible
10 * issue because of which request did fail.
11 *
12 * Request can be resent using: status.retry()
13 */
14 }
15})
1[self.client deleteMessagesFromChannel:@"channel" start:@15526611838554309 end:@15526611838554310
2 withCompletion:^(PNAcknowledgmentStatus *status) {
3
4 if (!status.isError) {
5 // Messages within specified time frame has been removed.
6 } else {
7 /**
8 * Handle message history download error. Check 'category' property to find out possible
9 * issue because of which request did fail.
10 *
11 * Request can be resent using: [status retry];
12 */
13 }
14}];
1
1
1
A PubNub timetoken exceeds the largest integer a JavaScript Number represents exactly, so increment or compare timetokens with an arbitrary-precision integer type such as BigInt rather than with plain numeric arithmetic.
Effect on usage metrics
Related tasks
- Retrieve message history. Check what messages exist in a channel before targeting a deletion range.
- Message Persistence. How storage, retention, and the timetoken model work.