Skip to content

Instantly share code, notes, and snippets.

@femdiya
Last active July 28, 2025 02:39
Show Gist options
  • Save femdiya/e669ef5f8b74f9a0a023849ab094ab33 to your computer and use it in GitHub Desktop.
Save femdiya/e669ef5f8b74f9a0a023849ab094ab33 to your computer and use it in GitHub Desktop.
Telethon script to delete all messages from a certain channel/group/user.
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())
@femdiya
Copy link
Author

femdiya commented Jul 28, 2025

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.

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