Skip to content

Instantly share code, notes, and snippets.

@wiz64
Last active December 18, 2024 02:33
Show Gist options
  • Save wiz64/eccab4158037238dc77b03d8c01cbf83 to your computer and use it in GitHub Desktop.
Save wiz64/eccab4158037238dc77b03d8c01cbf83 to your computer and use it in GitHub Desktop.
Telegram Leave All groups and Channels Python Script
# Python script that leave all groups and channel on your account
# useful to clean up crowded telegram accounts with hundreds of groups joined
# Developer : wiz64
# https://gist.github.com/wiz64/eccab4158037238dc77b03d8c01cbf83
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import telethon
# Go to https://my.telegram.org/apps, sign in, go to API development tools, create an app, copy and paste below:
api_id = '' # your id here
api_hash = '' # your hash here
phone = '' # your phone here
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
try:
client.sign_in(phone, input('Enter the code: ')) # Enter the login code sent to your telegram
except telethon.errors.SessionPasswordNeededError:
password = input("Enter password: ")
client.sign_in(password=password)
chats = []
last_date = None
chunk_size = 200
groups=[]
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash = 0
))
chats.extend(result.chats)
"""
Megagroups are groups of more than 200 people, if you want to leave
smaller groups as well delete this part. If you want to stay in a few
specific groups, add their titles to the groups_to_exclude list.
"""
groups_to_exclude = ['group title']
for chat in chats:
try:
if chat.megagroup== True and chat.title not in groups_to_exclude:
client.delete_dialog(chat)
except:
continue
@ker00sama-dev
Copy link

ker00sama-dev commented Jul 10, 2024

updated version

from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
from telethon.errors import UserNotParticipantError, ChatAdminRequiredError, ChannelPrivateError
import telethon

api_id = '**************'
api_hash = '********************************'
phone_number = '+************'

client = TelegramClient(phone_number, api_id, api_hash)

client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone_number)
    try:
        client.sign_in(phone_number, input('Enter the code: '))  # Enter the login code sent to your telegram
    except telethon.errors.SessionPasswordNeededError:
        password = input("Enter password: ")
        client.sign_in(password=password)

chats = []
last_date = None
chunk_size = 200

result = client(GetDialogsRequest(
    offset_date=last_date,
    offset_id=0,
    offset_peer=InputPeerEmpty(),
    limit=chunk_size,
    hash=0
))
chats.extend(result.chats)

for chat in chats:
    try:
        # Check if the chat is a group or megagroup
        if hasattr(chat, 'megagroup') and chat.megagroup or hasattr(chat, 'broadcast') and chat.broadcast:
            client.delete_dialog(chat)
            print(f"Deleted group: {chat.title}")
    except UserNotParticipantError:
        print(f"Failed to delete {chat.title}: The target user is not a member of the specified megagroup or channel.")
    except ChatAdminRequiredError:
        print(f"Failed to delete {chat.title}: The target user is not an admin of the specified megagroup or channel.")
    except ChannelPrivateError:
        print(f"Failed to delete {chat.title}: The channel specified is private and you lack permission to access it. Another reason may be that you were banned from it.")
    except Exception as e:
        print(f"Failed to delete {chat.title}: {e}")

client.disconnect()


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