Skip to content

feat(messages): delete messages with tg messages delete - #190

Open
ertembiyik wants to merge 15 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/messages-delete
Open

ertembiyik wants to merge 15 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/messages-delete

Conversation

@ertembiyik

@ertembiyik ertembiyik commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What and why

tg messages delete --chat <peer> --id <id>... [--revoke] deletes messages of a chat, in the shape #23 proposes. Refs #23

The need: deleting meant a raw tg api messages.deleteMessages call, which names no chat at all, and the cache kept showing the message.

Before (0.14.0)

$ tg messages delete --chat me --id 1
USAGE tg messages list|get|search|wait|watch|unread|read
…
Unknown command delete

After (real output of the e2e fake, whitespace collapsed)

$ tg messages delete --chat @alice --id 12 --id 13 --revoke
{ "success": true, "data": { "chatId": 10, "messageIds": [12, 13], "deletedCount": 2, "forEveryone": true } }

An ID of another chat is refused, and nothing is deleted. Message 40 is in Bob's chat:

$ tg messages delete --chat @alice --id 9 --id 40
{ "success": false, "error": { "code": "TELEGRAM_ERROR", "message": "Chat 10 has no message 40: nothing was deleted", "details": { "chatId": 10, "missingIds": [40] }, "retryable": false } }

How it works

  • Two methods. A supergroup or channel: channels.deleteMessages, always for everyone. A private chat, a basic group or Saved Messages: messages.deleteMessages, for this account only unless --revoke.
  • The ID check, and why. messages.deleteMessages takes no peer: private chats and basic groups share one message ID sequence per account, so Telegram would delete message N in whatever chat holds it. That would go past --chat, and past allowedChats, which checks --chat. So for these chats the command first fetches the IDs (messages.getMessages) and deletes only if every one is a message of --chat. Otherwise it deletes nothing; the error lists the IDs and doesn't say which chat holds them.
  • The cache follows. The rows are marked deleted, as the daemon does for a delete update, so list and search drop them unless --includeDeleted.
  • Fewer deleted than asked. deletedCount is Telegram's ptsCount. When it is lower than the number asked, the command fetches the IDs once more and marks only the ones that are gone, so the cache never hides a message that still exists.
  • Limits and access. Up to 100 IDs per call, more is INVALID_ARGS before connecting; a repeated ID counts once. It is a write with chatArgs: ['chat']: read-only mode refuses it and allowedChats applies.

Telegram's refusals are TELEGRAM_ERROR with the name in details.telegramError, as in #187: MESSAGE_DELETE_FORBIDDEN, MESSAGE_ID_INVALID/MSG_ID_INVALID, CHAT_ADMIN_REQUIRED, CHANNEL_PRIVATE (names from the two methods' docs pages).

Decisions you may want to change:

  • Without --revoke the delete is for this account only, the less destructive default; forEveryone in the output says what happened.
  • The wrong-chat refusal is TELEGRAM_ERROR like "message not found", and doesn't tell a missing ID from one in another chat.
  • No confirmation prompt: the CLI is non-interactive. The help text and docs say a delete can't be undone.
  • One extra call (messages.getMessages) per delete outside channels, for the ID check.

Left out: deleting a chat's whole history, and a date range.

This is one of four PRs for #23, one command each: edit (#187), delete, forward, pin. Each is cut from main and stands alone. All four touch src/commands/messages.ts, src/commands/access.ts, the shared fake client (src/__e2e__/helpers/fake-client-cli.ts), the README row for #23 and the CHANGELOG, so each one that lands conflicts with the rest in those spots; I merge main into the others as you land them.

Update: main merged in (5443e73)

  • main moved to oxfmt and oxlint with the landmines rules, and moved every test beside its code. This branch follows: the same behaviour and output, the code restyled, prose comments and explicit return types removed.
  • Tests are now at src/test/messages-delete.e2e.test.ts and src/commands/messages/delete-messages.test.ts; the fake client is src/test/fake-client-cli.test-utils.ts and the live step is in src/test/telegram.integration.test.ts. Paths and totals elsewhere in this description predate the move.
  • The reason for the ID check before a delete is kept in the code as the one comment the rules allow (// WARNING: …): messages.deleteMessages names no chat, so an ID of another chat would be deleted there.
  • .oxlintrc.json: the allowed count of raised timeouts in src/test/telegram.integration.test.ts goes from 7 to 8, for the live step this PR adds. Each of feat(messages): edit a sent message with tg messages edit #187, feat(messages): delete messages with tg messages delete #190, feat(messages): forward messages with tg messages forward #191 and feat(messages): pin and unpin a message with tg messages pin and unpin #192 does this, so the count needs one more per PR that lands.
  • After the merge: bun run check clean (format, lint, types, Knip), bun run test 1204 pass, bun run test:e2e 351 pass, 0 fail. Totals are lower than before because main merged and removed many tests.

How I checked it

  • E2E with the fake client (11 tests): with and without --revoke; several and repeated IDs, then list and search hiding them and --includeDeleted showing them; a supergroup; Saved Messages and a basic group; Telegram deleting fewer than asked, with only the gone ones hidden; another chat's ID and a missing ID refused with no delete call; a Telegram refusal with the cache untouched; a bad --id and 101 IDs refused offline; no credentials. The access suite adds read-only and allowedChats refusals from the table.

  • Unit tests (9): the request for each kind of peer, the ID check, each refusal, other errors passed on.

  • Live, by hand, on a personal account in Saved Messages: messages.getMessages returned the chat of each message as the check expects (a Saved Messages message under the own ID, a message of a private chat under that chat). An ID that exists nowhere was refused and the real message beside it stayed. Four test messages were then deleted in one call: deletedCount: 4, all four messageEmpty on Telegram, hidden in search and isDeleted with --includeDeleted.

  • A step in the live smoke suite (src/__integration__/). Written, not run: I have no test session.

  • Not verified live: --revoke in a chat with another person, a supergroup or channel, and Telegram deleting fewer than asked.

  • bun run check: clean. bun run test: 2356 pass, 0 fail. bun run test:e2e: 467 pass, 0 fail.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard (docs/cli-commands.md, docs/configuration.md, docs/api-design.md, README, CHANGELOG)

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

Update: second code review (0046add)

A second, independent review of this branch. Each point below was first shown by a failing test, then fixed.

  • --revoke false deleted for everyone: citty reads --revoke as true and drops false as a positional. The command now refuses, with INVALID_ARGS (exit 3) and before anything is sent, any word it does not expect and any --revoke=<value> other than true or false (--revoke=no was read as true too). --revoke=false and --no-revoke leave it off.
  • An empty ID list (--no-id) is INVALID_ARGS; it used to reach Telegram.
  • Every delete is now followed by the check of which messages are gone. The shortcut that skipped it when ptsCount equalled the number of IDs is removed: ptsCount counts update events and names no IDs. One more request per delete.
  • The result has deletedIds (the IDs Telegram no longer has) and unverifiedIds (every ID when that check failed, for example on a flood wait; the delete has still happened and the cache stays as it was).
  • Shared code on main that the review also pointed at, unchanged here: the access guard and the command resolve the account and the chat separately; a cached username can outlive its owner; flags a command does not declare are ignored; the activity log write after a successful request can turn it into a failure.

After these commits: bun run check clean, bun run test 1204 pass, bun run test:e2e 363 pass, 0 fail.

Update: main merged in (649eef9)

main up to #220 merged in, no conflicts. One follow-up commit (5b11263):

  • The command's own check of stray arguments is removed: the guard from fix: act on the checked target, refuse undeclared flags, keep done calls done #220 now refuses a flag a command does not declare and a word it would drop, for every command, so --revoke false and a bare word are refused there. This replaces what the section above says about it.
  • Kept: a --revoke=<value> other than true or false is INVALID_ARGS before anything is sent. The guard lets it through and citty reads --revoke=no as true. The error no longer has details.arguments.
  • No longer refused: words after a bare --. The guard stops there and citty ignores them, so -- --revoke deletes for this account only.
  • The command acts on the chat the allowedChats check resolved (checkedChat), as tg send and tg messages read do since fix: act on the checked target, refuse undeclared flags, keep done calls done #220. No test of its own: the handoff is tested on main for tg send; here the command's e2e file was run once by hand with allowedChats set, and every case whose chat was in the list passed.

After the merge: bun run check clean, bun run test 1215 pass, bun run test:e2e 361 pass, 0 fail.

Update: main merged in (853a651)

main up to 0.15.1 and #232 merged in. Conflicts: CHANGELOG.md (the entry of this PR now sits under the new ## [Unreleased]), the command index of docs/cli-commands.md and the fake client of the e2e tests.

After the merge: bun run check clean, bun run test 1227 pass, bun run test:e2e 387 pass, 0 fail.

Update: main merged in (ac85803)

main up to 0.15.2 (#234) merged in. One conflict: CHANGELOG.md (the entry of this PR stays under ## [Unreleased], above the new 0.15.2 section). No code change in this PR.

After the merge: bun run check clean, bun run test 1227 pass, bun run test:e2e 387 pass, 0 fail.

Update: main merged in (392c8d8)

Besides the conflicts in CHANGELOG.md, README.md and src/test/fake-client-cli.test-utils.ts, flagValues on main now takes the command's flag definitions, so delete.ts passes them; with that, a value given to another string flag is no longer read as an --id, as on main. Checks: bun run check clean, unit 1347 pass, e2e 460 pass, 0 fail.

Update: the ID check runs in supergroups and channels too (ddb64bb)

A review found that in a supergroup or channel an ID the chat never had was reported as deleted: channels.deleteMessages accepted it, the fetch after the delete did not find it, and it came back in deletedIds with deletedCount: 0 and exit 0. The check described under "The ID check, and why" ran only before messages.deleteMessages.

It now runs before both methods (channels.getMessages in a supergroup or channel): an ID the chat does not have refuses the whole call with details.missingIds, and nothing is deleted. This supersedes "One extra call … outside channels" above: every delete makes that call. A message that was already gone is now refused instead of counted as a success.

E2E: refuses an ID a supergroup does not have, and deletes nothing. After the change: bun run check clean, bun run test 1347 pass, bun run test:e2e 461 pass, 0 fail.

🤖 Generated with Claude Code

ertembiyik and others added 5 commits November 18, 2026 15:09
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. README and CHANGELOG: both sides kept. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
main now has neoromantic#195: Knip in bun run check, and long as a runtime dependency. No conflict; the check is clean.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
main now has neoromantic#196, a test time budget only. No conflict; the check is clean.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
README, docs/cli-commands.md, access.ts and messages.ts conflicted only with main's oxfmt reformat and were resolved by keeping main's text with this branch's delete lines; the delete's e2e, unit and integration tests and its fake-client and access additions moved to main's colocated layout (src/test/*, *.test-utils.ts, delete-messages.test.ts beside its module). The merge also brought the code under main's oxlint/landmines rules (no prose comments, inferred return types, the chat-scoping rationale kept as a WARNING line), reused createAccountCacheDb and raised the integration file's allowed raised-timeout count to 8.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik and others added 2 commits November 19, 2026 19:56
…ges delete

citty reads `--revoke false` as --revoke plus an ignored positional, and
`--revoke=no` or `--revoke=0` as true, so each deleted for everyone.
`--no-id` sent an empty ID list on to Telegram. Both are now
INVALID_ARGS before the chat is resolved; the global --format value
after the command stays allowed.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ptsCount is the number of update events a call generated, not a list of
deleted IDs, so the shortcut that marked every ID deleted when it
matched the count is gone: the command always asks Telegram which
messages are gone. When that check fails after a confirmed delete, the
command still succeeds, leaves the cache as it was and reports
deletedIds: [] with every ID in unverifiedIds.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@ertembiyik

Copy link
Copy Markdown
Contributor Author

From a second review:

  • --revoke false deleted for everyone (citty reads --revoke as true and drops false). It is INVALID_ARGS now, before anything is sent.
  • Every delete is now followed by one request that checks which messages are gone; the ptsCount shortcut is removed, since it counts events and names no IDs. The result has deletedIds and unverifiedIds.

Say if the extra request or the two new fields aren't what you want.

ertembiyik and others added 2 commits November 19, 2026 22:12
…red check

The guard now refuses undeclared flags and stray words for every command, so
delete keeps only the --revoke=<value> refusal the guard lets through, and
acts on the chat the allowedChats check resolved.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik and others added 6 commits November 20, 2026 20:19
…ssages-delete

# Conflicts:
#	CHANGELOG.md
#	docs/cli-commands.md
#	src/test/fake-client-cli.test-utils.ts
…ssages-delete

Pass the delete command's arg definitions to flagValues, which now takes
them to skip a string flag's value that looks like a repeated --id.

# Conflicts:
#	CHANGELOG.md
#	README.md
#	src/test/fake-client-cli.test-utils.ts

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The check that every ID is a message of --chat ran only before
messages.deleteMessages. In a supergroup or channel an ID the chat never
had passed straight to channels.deleteMessages, was missing in the fetch
after it, and so came back in deletedIds with exit 0. The check now runs
for both methods: the call is refused with details.missingIds and nothing
is deleted.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant