Skip to content

Instantly share code, notes, and snippets.

@eramax
Created March 8, 2023 15:06
Show Gist options
  • Save eramax/2a651152cbbb4c73d9b9e63e869abea9 to your computer and use it in GitHub Desktop.
Save eramax/2a651152cbbb4c73d9b9e63e869abea9 to your computer and use it in GitHub Desktop.
Remove telegram groups
# Remove telegram groups
# python3 -m pip install --upgrade telethon
from telethon import TelegramClient, sync
# get an Id from https://my.telegram.org/apps
api_id = 111111111111
api_hash = 'xxxxxxxxxxxxx'
# List all the group names you want to keep, the script will delete other groups.
excluded_groups = ['gp1','gp2']
class TelegramAPIs(object):
def init_client(self, session_name, api_id, api_hash):
self.client = TelegramClient(session_name, api_id, api_hash)
self.client.start()
def close_client(self):
if self.client.is_connected():
self.client.disconnect()
def clean(self):
for dialog in self.client.get_dialogs():
if hasattr(dialog.entity, "title") and dialog.entity.title not in excluded_groups:
print('----- deleting ', dialog.entity.title)
self.client.delete_dialog(dialog.entity)
ta = TelegramAPIs()
ta.init_client(session_name='session_name', api_id=api_id, api_hash=api_hash)
ta.clean()
ta.close_client()
@sbuvaneshkumar
Copy link

Thanks :), worked like a charm 💯

@elevatedesigner
Copy link

how to find api hash

@eramax
Copy link
Author

eramax commented Apr 19, 2024

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