# Chapter 18: Recall and edit

> Ana posts something wrong in the hiking group and recalls it a minute later. The message is already on dozens of devices, many more haven’t pulled it yet, and its notification is still lit on lock screens. How do you change every copy?

IM Systems in Depth · https://im.liko.page/en/recall-and-edit/

Ana posts “Saturday’s meeting time moves to 8” in the hiking group, realises only afterwards that it’s wrong, and taps recall a minute later.

By now the message is not one copy. The hiking group has 500 people with 1.5 devices each on average: **750 copies**. Some phones already show it, most haven’t pulled it yet, and its notification is still lit on lock screens. A recall has to change every copy, including on devices that are offline right now.

## 1. First, what does the product need?

| Case | On screen | What the server does |
|---|---|---|
| Delete for me only | Gone from my devices, unchanged for everyone else | Kept on me alone; not this chapter |
| Recall | Replaced in place by “Ana recalled a message”, or simply gone | This chapter |
| Edit | Replaced by the new text, marked “edited” | The same way as recall |
| An admin recalls someone else’s | Same as recall | The same way, with a different permission check |

Recall and edit both have time limits, set by the product: when WhatsApp launched “delete for everyone” in 2017 the limit was 7 minutes ([blog](https://blog.whatsapp.com/deleting-messages-for-everyone)), and edits are allowed within 15 minutes of sending ([blog](https://blog.whatsapp.com/now-you-can-edit-your-whatsapp-messages)); Telegram’s limits are sent from the server to the client (`edit_time_limit` and `revoke_time_limit` in [`config`](https://core.telegram.org/constructor/config)). The server has to check the limit; the client alone is not enough.

## 2. The first idea: change it on the server

Call the wrong message x. The most direct way: the server runs `UPDATE messages SET recalled = 1`, then pushes “x was recalled” to the group’s devices that are online right now.

This does work for some devices: those that are offline will pull later, and what they pull is already changed. The trouble is a different kind of device: **one that already has the message but misses that notice**. Ben’s phone, say, was open when the message arrived and locked by the time of the recall.

Why can it never be put right? Chapter 6’s sync pulls only seqs above the sync cursor, and this message’s seq is long behind the cursor. Since chapter 7 the screen reads only the local database, and opening the conversation doesn’t ask the server again. So the original stays on his phone for good.

Making the notice reliable isn’t enough either: adding acks and resends covers devices online at the time of the recall; those offline then still miss it. To reach them too, the notice would have to be stored for each device until it comes to collect it. That is chapter 6’s inbox (a copy kept for each recipient until they collect it), built just for one kind of message, the recall, and running beside the sync that messages already use.

Work it through for a peak hour (10% online, 10% of pushes lost):

- **Recalled after one minute:** about 87 devices already have the original. Changing it on the server and notifying those online, on average **24 devices** keep the original for good; with acks and resends on the notice, still 17.
- **Recalled after one hour:** about 620 devices have the original, and **553** keep it for good.

## 3. A recall takes a seq too

The problem: a recall changes an old message but leaves nothing on the line of seqs, and every device only looks forward along that line. Chapter 12’s joins and leaves were solved the same way: **make the recall take a seq, like a message.**

**On the server, one transaction:**

```sql
BEGIN;
UPDATE conversations SET last_seq = last_seq + 1 WHERE id = :g RETURNING last_seq;   -- 4,931; locks the conversation's row
-- check: x (seq 4,930) was sent by Ana (or she is an admin), is within the time limit, and is not already recalled
INSERT INTO messages (conversation_id, seq, sender_id, kind, target_seq) VALUES (:g, 4931, :ana, 'recall', 4930);
UPDATE messages SET recalled_by = 4931, text = '' WHERE conversation_id = :g AND seq = 4930;
COMMIT;
```

From there it takes the same road as an ordinary message: chapter 11’s **to-do** (a row written when a message is sent, saying “update every member’s conversation record”, later done in the background one small transaction per person) updates each member’s row; online devices get a push, offline ones get it at their next sync. Like a join, it goes to the whole group: a seq that someone doesn’t receive is a gap on their phone, and the phone would go and fetch it (chapter 5).

**On the phone, one rule:** when a recall arrives, mark x’s row as recalled, committed in the same transaction as the cursor (chapter 7). The open chat protocol Matrix asks the same of clients: when one receives a recall (a “redaction” there), it must change the original message the way the server did ([redactions](https://spec.matrix.org/latest/client-server-api/#redactions)).

One part of this rule needs care: **a recall can arrive before the message it recalls.** Both of these are common:

- Ben comes back from being offline. Chapter 6 pulls only the newest message of each conversation he is behind on, and the newest is this recall; x comes before it and has not been pulled yet.
- Ben is scrolling up. A page of messages including x is read on the server before the recall, and by the time it reaches the phone, the recall has already arrived.

So when x is not on the device, insert a placeholder row marked “recalled”. In chapter 7’s local table, a conversation can have only one row per seq; when the original arrives late and hits that row, it doesn’t overwrite the recalled one.

Pick “the recall takes a seq” in the figure above: no device keeps the original for good. An hour after the recall some still haven’t changed (about 5 on average for a recall after one minute, about 105 for a recall after one hour); they just haven’t opened the app yet, and will change at their next sync.

## 4. Edits: the same thing, only moving up

An edit is also a message that takes a seq, carrying the new text and pointing at x. The difference is that there can be several edits, and push and sync can deliver them out of order.

The fix is the same as chapter 14’s read position: x’s row records the seq of the last change applied to it (an edit or a recall), and **accepts only a higher one**. Say Ana edits twice: the first edit is 4,935, the second 4,940. The push for 4,940 arrives first and x records 4,940; 4,935 comes later through sync, is lower than 4,940, and changes nothing, so the screen keeps the second edit’s text.

When recalls and edits meet: edit then recall, and the recall has the higher seq, so it wins, and a late edit can’t cover it; recall then edit, and the server refuses the edit when it checks under the lock.

## 5. Recalling an unread message

Chapter 14’s unread count rests on two things: each message carries “the count so far” (how many messages up to this one in the conversation count as unread), and each sender has a running number (this is their Nth message in the conversation). Recalls and edits themselves are like system messages: they repeat the previous count, take no running number, and don’t count as unread.

But if Ana hasn’t read the recalled message, it should leave her unread count too. Continuing chapter 14’s example: Ana has read up to 4,820 and has 2 unread, 4,821 and 4,824. Ben recalls 4,821. In the sum below, 103 and 100 are the counts on the latest message and on the one she has read up to. 7 and 6 are her own running numbers at those same two points (the one message she sent in between is not unread):

> unread = (103 − 100) − (7 − 6) − **1** = **1**

That last 1 is the number of recalled messages from others above her read position. Each time the server works out the unread count (when sending the conversation record) it does one more lookup: an index on (conversation, recalled seq); recalls are rare, so the lookup mostly finds nothing.

- A recalled message she has already read is below her read position, so nothing is taken off: the unread count never included it.
- Ana recalling her own: her own messages are already taken off, so only others’ unread counts go down.

If x @mentioned anyone, chapter 14 keeps a row for each person mentioned; the recall’s to-do deletes those rows as it goes, and the @ beside the red dot goes away too.

## 6. Notifications already on the lock screen

Ben’s app wasn’t open, so when x arrived an OS push had already put “Ana: Saturday’s meeting time moves to 8” on his lock screen (chapter 17). In the one-minute case above, about 630 devices in the hiking group showed it, and about 610 still had it lit at the recall. These are not the 87 devices of section 2: those had the app open and synced x into the local database; these 630 had the app closed, and x appeared only in a notification shown by the OS (about 20 of them opened the app before the recall, so they are among the 87 too).

A notification is not in the app’s database, so sync can’t change it. The fix is to push again and replace it with **the same identifier**. Chapter 17 combines notifications by conversation, so the identifier is the conversation ID:

- iOS: notifications with the same `apns-collapse-id` are shown by the OS as one ([APNs](https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns)).
- Android (FCM): while a notification with the same `tag` is showing, the new one replaces the old ([FCM](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages)).

So what replaces it is a new summary of the conversation without the original text; the badge goes down too (chapter 17’s unread total, changed in the same transaction that changes the record). Chapter 17 also tells the server whom to send it to: it records the last notification time per device per conversation, and a device that hasn’t come to the foreground since then still has the notification on screen.

Like any OS push, it isn’t guaranteed to arrive: a notification is only a nudge, and what is in the conversation is what counts.

## 7. The numbers

- **The price of one recall:** the same as a message: one seq, one row, plus chapter 11’s conversation records (500 rows in the hiking group) and pushes.
- **In total:** if 1% of messages are recalled or edited, that is 40,000 a day, 1% more load.
- **Server storage:** the original text can be cleared; the recall’s row stays.

## 8. The cost

- **History only grows.** Every recall or edit adds a message.
- **Every client has to know the new message types.** An old version of the app doesn’t, and shows a message it can’t make sense of, or does nothing and keeps the original. When WhatsApp launched it, it said both sides had to be on the latest version for the deletion to work (chapter 31).
- **Copies outside the system can’t be recalled.** Screenshots, messages already forwarded, the few words copied into a quote. A quote should store only x’s seq and let the phone display it from the local row, so after a recall it shows “the original message was recalled”.
- **Replacing a notification is one more push,** and it isn’t guaranteed to arrive either.
- **People who left the group don’t get it.** For someone who left between x and the recall, their membership range (their stretch of seqs in the group, chapter 12) doesn’t include the recall’s seq, and the original stays on their phone.

## 9. Other answers

- **Change it on the server, notify those online.** Enough for a web client that stores no messages (chapter 7’s “store nothing”): it reads from the server every time.
- **Telegram:** deletes and edits are both events with a pts in the update stream, in the same sequence as new messages: [`updateDeleteMessages`](https://core.telegram.org/constructor/updateDeleteMessages), [`updateEditMessage`](https://core.telegram.org/constructor/updateEditMessage); supergroups use the group’s own sequence ([`updateDeleteChannelMessages`](https://core.telegram.org/constructor/updateDeleteChannelMessages)). An edited message keeps its id and gains an `edit_date`; on delete, `revoke` decides whether it is deleted for everyone ([`messages.deleteMessages`](https://core.telegram.org/method/messages.deleteMessages)).
- **Matrix:** a recall is an `m.room.redaction` event pointing at the original event; from then on the server returns only the original event with its content stripped. An edit is a new event with an `m.replace` relation; when there are several, the latest by server time wins ([edits](https://spec.matrix.org/latest/client-server-api/#event-replacements)).
- **Keep every version of an edit.** Edits are messages anyway, so keeping them gives an edit history; or show clients only the latest.

## 10. This chapter’s decision

**Decision card**

- Problem: Once sent, a message has 750 copies (500 people × 1.5 devices). Change it on the server and notify the online devices, and a device that has the original but misses the notice is already past it with its cursor and keeps the original for good: 24 devices on average for a recall after one minute, 553 after one hour.
- Choice: Recalls and edits take a seq in the conversation like messages, in the same transaction that marks the original; every device applies them the way it receives messages. If a recall arrives first, the phone inserts a placeholder row; an edit is accepted only with a higher seq; the unread count takes off recalled messages from others above the read position; a notification already shown is replaced using the same identifier.
- Cost: History only grows, and a recall costs as much as a message; every client has to know the new types; one more lookup to work out unread counts; screenshots, forwards and people who left can’t be reached; replacing a notification isn’t guaranteed to arrive.
- Revisit when: Old versions of the app (chapter 31); end-to-end encryption, where the server can’t change the original (chapter 42).
- Other answers: Change it on the server and notify those online (enough for a web client); Telegram’s updateDeleteMessages / updateEditMessage with a pts; Matrix’s redaction and m.replace; keep every version of an edit.

That completes v2’s nine chapters: the conversation list, members, read positions and receipts, several devices, waking a phone, recall. The next chapter walks through the whole of v2 and does the sums once more.
