Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,13 @@ ENABLE_LISTENER=true
# Batch sync alternative (expensive — prefer ENABLE_LISTENER instead)
SYNC_DELETIONS_EDITS=false
# Only re-check messages posted in the last N days (0 = every stored message).
# Older posts are checked again only on a run with 0.
# Older posts are checked again only on a run with 0, or by the rotation below.
SYNC_DELETIONS_EDITS_DAYS=0
# With SYNC_DELETIONS_EDITS_DAYS above 0, also re-check up to this many OLDER
# messages per run, rotating over the chats whose last full check is oldest, so
# every stored message is re-checked over several runs (0 = off). Telegram takes
# about 100 seconds per 1000 messages.
SYNC_DELETIONS_EDITS_OLDER_PER_RUN=0

# Re-download missing or corrupted media files
VERIFY_MEDIA=false
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ services:
# Batch sync alternative (expensive — prefer ENABLE_LISTENER instead)
SYNC_DELETIONS_EDITS: ${SYNC_DELETIONS_EDITS:-false}
SYNC_DELETIONS_EDITS_DAYS: ${SYNC_DELETIONS_EDITS_DAYS:-0}
SYNC_DELETIONS_EDITS_OLDER_PER_RUN: ${SYNC_DELETIONS_EDITS_OLDER_PER_RUN:-0}
VERIFY_MEDIA: ${VERIFY_MEDIA:-false}
INTERNAL_PUSH_SECRET: ${INTERNAL_PUSH_SECRET:-}
# Split containers: realtime pushes must target the viewer SERVICE, not
Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ For upgrade instructions, see [Upgrading](#upgrading) at the bottom.

## [Unreleased]

### Added
- **Older messages can be re-checked a slice per run.** With `SYNC_DELETIONS_EDITS_DAYS` above 0, edits and deletions of older posts were caught only by a run with `SYNC_DELETIONS_EDITS_DAYS=0`. On a large archive that one run takes many hours, mostly waiting on Telegram's rate limit, and it grows with every chat. `SYNC_DELETIONS_EDITS_OLDER_PER_RUN=N` also re-checks up to N messages older than the window on each run, for each account, shared by its chats. Each request to Telegram counts as 100 of them, so a run makes at most N / 100 requests, rounded up. The chats whose last full check is oldest go first, and a large chat continues on the next run from the message where it stopped. When a chat's check reaches its oldest message, the time is recorded and the chat goes to the back of the line. A request that fails with an error other than a FloodWait is skipped and tried again on the chat's next check. So every archived message is re-checked over several runs, without one very long run and without changing `CHAT_IDS`. Telegram takes about 100 seconds per 1000 messages. The default, `0`, changes nothing. Migration 044 adds the progress to `sync_status`. See [Re-checking older messages over several runs](configuration/schedule.md#sync-rotation). Thanks to @Scalpinho for #641.

## [9.9.0] - 2026-11-23

A contact who deletes their Telegram account keeps the name the archive had, the viewer marks that account as deleted, and chat search finds a full name or an `@handle`. Migration 043 runs on start. Docker deployments only need the new pins.
Expand Down
4 changes: 2 additions & 2 deletions docs/configuration/listener.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ A short network blip that the Telegram library reconnects by itself is covered t
Some things are not replayed:

- A restart after a one-shot command. Any command that connects with the same session (see [One client per session](../getting-started/telegram-login.md#one-client-per-session)) saves the current update state into it, so a listener started after it has nothing left to replay. Edits and deletions made before that are recorded only if `SYNC_DELETIONS_EDITS` is on (off by default).
- A very long outage. When Telegram answers that too much was missed, for the account or for one chat (roughly 100 or more updates in one supergroup or channel), the listener drops that replay and logs a warning that contains `replay as too long`. It names neither the chat nor its id, unless `LOG_CHAT_TITLES=true`, which adds the channel's title and id: `replay as too long: "Demo News" (id -1001234567890) (pts A→B)`. With it on, remove them from logs before you paste them anywhere. New messages from the gap still arrive with the next backup. Edits and deletions in the gap come back only if `SYNC_DELETIONS_EDITS` is on (off by default), and with `SYNC_DELETIONS_EDITS_DAYS` above 0 only for messages posted inside that window, see [Checking only recent messages](schedule.md#sync-window). A message posted and deleted inside the gap cannot be recovered. The listener asks for the replay of every saved channel it has an access hash for as soon as the saved state is loaded, instead of 15 minutes later. A channel that Telegram rate-limits then (a FloodWait) is retried 15 minutes later, so on an account with many channels its gap can still grow past the limit.
- A very long outage. When Telegram answers that too much was missed, for the account or for one chat (roughly 100 or more updates in one supergroup or channel), the listener drops that replay and logs a warning that contains `replay as too long`. It names neither the chat nor its id, unless `LOG_CHAT_TITLES=true`, which adds the channel's title and id: `replay as too long: "Demo News" (id -1001234567890) (pts A→B)`. With it on, remove them from logs before you paste them anywhere. New messages from the gap still arrive with the next backup. Edits and deletions in the gap come back only if `SYNC_DELETIONS_EDITS` is on (off by default), and with `SYNC_DELETIONS_EDITS_DAYS` above 0 only for messages posted inside that window, or later when [the rotation of older messages](schedule.md#sync-rotation) reaches them, see [Checking only recent messages](schedule.md#sync-window). A message posted and deleted inside the gap cannot be recovered. The listener asks for the replay of every saved channel it has an access hash for as soon as the saved state is loaded, instead of 15 minutes later. A channel that Telegram rate-limits then (a FloodWait) is retried 15 minutes later, so on an account with many channels its gap can still grow past the limit.
- A stop during the replay. `docker stop` while the replay runs can lose the updates still being handled; the library saves its state as it shuts down.
- Reactions. They carry no update counter, so a restart does not replay them; the reaction re-sweep (`REACTION_RESWEEP_DAYS`) covers them.
- Reactions you add from another device, which Telegram does not always push. See [Reactions](#reactions).
Expand Down Expand Up @@ -161,7 +161,7 @@ Every deletion event that had deletions blocked logs one warning with their mess
🛡️ Rate limiter blocked 3 deletion(s), not applied, message ids: 5011, 5012, 5013. SYNC_DELETIONS_EDITS is off, so they stay unrecorded; enable it for a backup run to record them
```

The `SYNC_DELETIONS_EDITS` pass of the scheduled backup re-reads every archived message and records the deletions the listener blocked. With it off, nothing else records them. With [`SYNC_DELETIONS_EDITS_DAYS`](schedule.md#sync-window) above 0, the pass reads only messages posted in the last N days. A blocked deletion of an older post is recorded only by a full pass, with `SYNC_DELETIONS_EDITS_DAYS=0`, and the warning says so.
The `SYNC_DELETIONS_EDITS` pass of the scheduled backup re-reads every archived message and records the deletions the listener blocked. With it off, nothing else records them. With [`SYNC_DELETIONS_EDITS_DAYS`](schedule.md#sync-window) above 0, the pass reads only messages posted in the last N days. A blocked deletion of an older post is recorded by a run with `SYNC_DELETIONS_EDITS_DAYS=0`, or, with [`SYNC_DELETIONS_EDITS_OLDER_PER_RUN`](schedule.md#sync-rotation) set, when the rotation of older messages reaches it. The warning says which.

`telegram-archive status --json` and `GET /api/status` show, per account, `deletions_blocked`, the deletions blocked since the listener last started, and `last_rate_limit_at`, when the limiter last tripped. The count starts again at 0 each time the listener starts, a watchdog restart included; `last_rate_limit_at` is kept across restarts. The shutdown summary logs the same count as **Deletions blocked (not applied)**.

Expand Down
56 changes: 48 additions & 8 deletions docs/configuration/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ For each account, in order:
5. Refresh pinned messages, up to 100 per chat.
6. Re-check recent reactions, if the [reaction re-sweep](#reaction-re-sweep) is on.
7. Back up archived chats that the main loop did not already cover.
8. Reconcile group-to-supergroup migrations.
9. Refresh [forum topics and folders](#forum-topics-and-folders).
10. Recalculate statistics.
11. Retry failed media downloads. See [Media downloads](media.md).
12. Verify media files, if `VERIFY_MEDIA` is on.
13. Send pending media for [transcription](transcription.md).
8. Re-check a slice of older messages, if [`SYNC_DELETIONS_EDITS_OLDER_PER_RUN`](#sync-rotation) is set.
9. Reconcile group-to-supergroup migrations.
10. Refresh [forum topics and folders](#forum-topics-and-folders).
11. Recalculate statistics.
12. Retry failed media downloads. See [Media downloads](media.md).
13. Verify media files, if `VERIFY_MEDIA` is on.
14. Send pending media for [transcription](transcription.md).

The run clears the "backup in progress" flag at the end, even when it fails.

Expand All @@ -126,7 +127,7 @@ Older messages are never read again by a normal run. Edits and deletions of mess
| Option | What it catches | Cost |
|--------|-----------------|------|
| [Real-time listener](listener.md) | Edits, deletions, new messages and reactions as they happen | A process that stays connected. |
| [`SYNC_DELETIONS_EDITS`](#sync_deletions_edits) | Edits and deletions of every archived message, or of the last N days with [`SYNC_DELETIONS_EDITS_DAYS`](#sync-window) | Re-reads the whole archive, or the window, on every run. Telegram answers about 300 ids per 30 seconds, so a million messages take more than a day. |
| [`SYNC_DELETIONS_EDITS`](#sync_deletions_edits) | Edits and deletions of every archived message, or of the last N days with [`SYNC_DELETIONS_EDITS_DAYS`](#sync-window), plus a slice of older messages per run with [`SYNC_DELETIONS_EDITS_OLDER_PER_RUN`](#sync-rotation) | Re-reads the whole archive, or the window and the slice, on every run. Telegram answers about 300 ids per 30 seconds, so a million messages take more than a day. |
| [`REACTION_RESWEEP_DAYS`](#reaction-re-sweep) | Reaction changes on recent messages | Up to 500 messages per chat per run, spaced out. |
| [`FILL_GAPS`](#gap-fill) | Messages missing between two archived messages | One scan of the stored ids per chat after each backup run. |

Expand All @@ -152,7 +153,46 @@ SYNC_DELETIONS_EDITS=true
SYNC_DELETIONS_EDITS_DAYS=14
```

The window is a deliberate blind spot. Edits, deletions, poll, preview and reaction changes on older posts are not seen, including changes made while the archive was down. To catch them, run a full pass: set `SYNC_DELETIONS_EDITS_DAYS=0`, or unset it, and recreate the container. With Docker Compose that is `docker compose up -d`: `docker compose restart` keeps the old environment. The [`schedule`](#the-schedule-command) command runs one backup straight away on start. Set the window back once that run ends.
The window is a deliberate blind spot. Edits, deletions, poll, preview and reaction changes on older posts are not seen, including changes made while the archive was down. There are two ways to catch them:

- Re-check them a slice per run with [`SYNC_DELETIONS_EDITS_OLDER_PER_RUN`](#sync-rotation). No run takes much longer than the others.
- Run one full check: set `SYNC_DELETIONS_EDITS_DAYS=0`, or unset it, and recreate the container. With Docker Compose that is `docker compose up -d`: `docker compose restart` keeps the old environment. The [`schedule`](#the-schedule-command) command runs one backup straight away on start. Set the window back once that run ends. On a large archive this one run takes many hours.

#### Re-checking older messages over several runs {#sync-rotation}

[`SYNC_DELETIONS_EDITS_OLDER_PER_RUN`](../reference/environment-variables.md#sync_deletions_edits_older_per_run) spreads the check of older messages over many runs. It works only with `SYNC_DELETIONS_EDITS=true` and `SYNC_DELETIONS_EDITS_DAYS` above 0.

```bash
SYNC_DELETIONS_EDITS=true
SYNC_DELETIONS_EDITS_DAYS=14
SYNC_DELETIONS_EDITS_OLDER_PER_RUN=20000
```

Each run still checks the window of every chat. After all chats are backed up, the run re-checks up to `SYNC_DELETIONS_EDITS_OLDER_PER_RUN` messages posted before the window. The number applies to each account on its own, and the accounts run one after another. Within an account, all its chats share it:

1. The chats that never finished a full check go first. Then the chat whose last full check is oldest. In each of these two groups, a chat in the middle of a check goes before one that has not started. Otherwise the order is fixed by chat id, so a large chat can take many runs, and the next chat starts when it finishes. The whole archive takes the same number of runs in any order.
2. In each chat, the check goes from the newest older message down, 100 ids per request. Each request counts as 100 messages of the budget, even when a small chat has fewer, because Telegram limits requests, not messages. So a run makes at most one request per 100 of the budget, rounded up, however many small chats it reaches.
3. A chat that has more older messages than the run has left continues on the next run from the same message.
4. When a chat's check reaches its oldest message, the time is recorded. The chat goes to the back of the line, and its next check starts again from the newest older message.
5. A run visits each chat once at most. When every chat has had its turn, the rotation ends, even with budget left.

Only the chats that this run backed up take part. A chat that leaves `CHAT_IDS` or is excluded keeps its place, and continues from there if it comes back. A chat added to the selection has never finished a check, so it goes first. Each account keeps its own progress per chat.

The progress is saved after every request. If the run stops, for example after a crash, at most 100 messages are checked again. If Telegram asks for a longer wait than the [retry](#rate-limits-and-retries) allows, the rotation stops for this run, and the next run continues from the same message. Any other error skips that request's 100 messages, the same way the window pass does: the check carries on below them, the run log counts them, and the next pass of that chat tries them again. A database error for one chat keeps its progress, and the other chats still get the rest of the budget.

The run log ends the step with one line, for example:

```text
Older-message rotation: re-checked 20000 older messages in 3 chat(s), 2 deleted, 5 updated; 1 chat(s) finished a pass, 40 of 120 have not finished one yet
```

When a request failed, the line adds `N skipped after errors, re-checked next pass`. The count "have not finished one yet" covers only the chats this run backs up. When it reaches 0, each of those chats has finished one check. Messages of a skipped request, and ids that a misaligned answer from Telegram left out, wait for that chat's next check. A message that leaves the window while its chat's check is under way, above the point the check has reached, waits for the next check of that chat. The window pass checked it until then.

**How to size it.** Telegram takes about 100 seconds per 1000 messages, so 20,000 messages add about 35 minutes to a run for each account. Size it per account: to re-check an account's history once per period, divide its messages older than the window by the number of runs in that period, and use the largest result. `Total messages` at the end of a run counts every account together, so split it first. For example, an account with 1,500,000 older messages and one run a day re-checks every message once a month with about 50,000 per run, which adds about 85 minutes. With a run every 6 hours, 12,500 per run is enough, about 20 minutes each. Each account adds its own time, so two accounts that each use the whole budget take twice as long. An account with fewer older messages than the budget gets a full check on every run. Because a request counts as 100 (step 2), an archive with many small chats takes more runs per cycle than this division gives. The window's messages come on top of this.

Deletions follow `DELETION_MODE`, the same as on a run with `SYNC_DELETIONS_EDITS_DAYS=0`. With `hard`, a message Telegram no longer returns is removed from the archive, and the rotation now reaches older messages on routine runs. A chat whose old history the account can no longer read, while the chat itself is still backed up, has those older messages marked deleted, or removed with `hard`. A group that hides its history from new members, left and joined again, is one example.

Turning it off (`0`) stops the rotation and keeps the saved progress; turning it on again continues from there. With `SYNC_DELETIONS_EDITS_DAYS=0` every run already checks every message, so the rotation does not run.

### Reaction re-sweep

Expand Down
2 changes: 1 addition & 1 deletion docs/design/deleted-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ In short, we recommend C for the mark and H for finding the marked messages. [Th
Two paths can tell the archive that a message is gone. Both are off by default; the [settings reference](../reference/environment-variables.md#listen_deletions) has every flag named here.

- **The listener** (`LISTEN_DELETIONS=true`). While it runs, Telegram sends it an event for each deletion. In private chats and basic groups the event carries only message ids, not the chat, so the archive looks each id up in the account's chats and skips it when the id is in more than one. A mass-operation guard caps each chat at 10 deletions in 30 seconds (`MASS_OPERATION_THRESHOLD`, `MASS_OPERATION_WINDOW_SECONDS`): past that, the chat's deletions are dropped for the next 30 seconds.
- **The sync** (`SYNC_DELETIONS_EDITS=true`). During a backup run it asks Telegram for every archived message of a chat, 100 at a time, or only those posted in the last `SYNC_DELETIONS_EDITS_DAYS` days. A message Telegram no longer returns is marked deleted. When Telegram's answer does not line up with the ids asked for, the batch marks nothing and the next run tries again.
- **The sync** (`SYNC_DELETIONS_EDITS=true`). During a backup run it asks Telegram for every archived message of a chat, 100 at a time, or only those posted in the last `SYNC_DELETIONS_EDITS_DAYS` days, plus a slice of older ones per run with `SYNC_DELETIONS_EDITS_OLDER_PER_RUN`. A message Telegram no longer returns is marked deleted. When Telegram's answer does not line up with the ids asked for, the batch marks nothing and the next run tries again.

With `DELETION_MODE=soft`, the default, the row stays: `is_deleted` becomes 1 and `deleted_at` records the time. With `hard` the row is removed and there is nothing left to mark.

Expand Down
Loading
Loading