Last active
July 28, 2025 02:39
-
-
Save femdiya/e669ef5f8b74f9a0a023849ab094ab33 to your computer and use it in GitHub Desktop.
Telethon script to delete all messages from a certain channel/group/user.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from telethon import TelegramClient | |
| from telethon.tl.types import PeerChannel | |
| import asyncio | |
| api_id = xxxxxx # Replace with your API ID. | |
| api_hash = 'your_api_hash' # Replace with your API Hash. | |
| getting_entity_ID = 'publicgroupusername/publicchannelusername' # No @ symbol, enter username only. | |
| client = TelegramClient('session_name', api_id, api_hash) | |
| async def delete_my_messages(): | |
| await client.start() | |
| entity = await client.get_entity(getting_entity_ID) | |
| me = await client.get_me() | |
| async for msg in client.iter_messages(entity): | |
| if msg.sender_id == me.id: | |
| try: | |
| await client.delete_messages(entity, msg.id) | |
| print(f"Deleted message ID {msg.id}") | |
| except Exception as e: | |
| print(f"Failed to delete {msg.id}: {e}") | |
| await client.disconnect() | |
| asyncio.run(delete_my_messages()) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're using IDs, you can delete line 7 entirely and replace "getting_entity_ID" in line 14 with your actual ID.
For example, if the ID is 7005674652:
For a channel, prefix it with -100, like -1007005674652
For a group, prefix it with a single dash, like -7005674652
For other cases, use the ID as-is.