Kick and Ban All Members Bot — Telegram Supergroup Purge Utility
Purge regular members from Telegram groups and supergroups safely with administrator immunity, 60-second soft-kick auto-unbans, and automatic FloodWait rate-limit recovery via @kick_and_ban_all_members_bot.

Technical Architecture: Reverse-Engineering @kick_and_ban_all_members_bot
Standard Telegram Bot API HTTP endpoints cannot enumerate complete member lists in large supergroups without knowing every user ID in advance. To overcome this protocol constraint, I engineered @kick_and_ban_all_members_bot around a hybrid architecture combining a python-telegram-bot webhook command layer with an asynchronous Pyrogram MTProto worker running inside an isolated ThreadPoolExecutor.
+------------------+ +--------------------------+ +------------------------+
| Telegram Webhook | --------> | Command Handler Layer | --------> | ThreadPoolExecutor |
| (Bot API) | | (handlers.py) | | (Background Worker) |
+------------------+ +--------------------------+ +------------------------+
| |
[Admin Auth & Perms] v
+------------------------+
| Pyrogram MTProto Client|
| (Async Member Stream) |
+------------------------+
|
v
[1-Min Auto-Unban & FW]
1. Asynchronous MTProto Member Streaming
When a verified group administrator triggers /remove_all_members or /purge, the worker spins up an ephemeral, in_memory=True Pyrogram MTProto session scoped to the target chat_id and streams member records asynchronously:
async def purge_group_members_async(chat_id: int, status_message_id: int):
app = Client(
session_name=f"kick_bot_session_{abs(chat_id)}",
api_id=int(settings.TELEGRAM_API_ID),
api_hash=str(settings.TELEGRAM_API_HASH),
bot_token=str(settings.KICK_AND_BAN_BOT_TOKEN),
in_memory=True,
)
kicked_count = 0
skipped_admins = 0
async with app:
async for member in app.get_chat_members(chat_id):
user = member.user
if not user:
continue
# Immunity Safeguard: Skip admins, owners, and bots
if member.status in [ChatMembersFilter.ADMINISTRATORS, ChatMembersFilter.OWNER] or user.is_bot:
skipped_admins += 1
continue
# 1-minute temporary ban (soft kick)
until_date = int(time.time() + 60)
await app.ban_chat_member(chat_id, user.id, until_date=until_date)
kicked_count += 1
2. Soft-Kick Auto-Unban (until_date = now + 60s) & FloodWait Recovery
Instead of permanently blacklisting users, the worker sets until_date = int(time.time() + 60). Telegram removes the member from the group immediately and automatically lifts the ban restriction after 60 seconds, allowing purged users to rejoin later if invited.
When bulk moderation triggers Telegram's FloodWait rate limit, the retry loop catches the exception, updates the in-chat progress message with the exact backoff duration (fw.value), sleeps, and resumes without dropping the cursor:
banned = False
while not banned:
try:
until_date = int(time.time() + 60)
await app.ban_chat_member(chat_id, user.id, until_date=until_date)
kicked_count += 1
banned = True
except FloodWait as fw:
logger.warning(f"FloodWait encountered: sleeping for {fw.value} seconds...")
await app.edit_message_text(
chat_id=chat_id,
message_id=status_message_id,
text=f"Rate Limited by Telegram: Pausing for {fw.value}s before resuming..."
)
await asyncio.sleep(fw.value)
Setup Guide & Command Reference
- Add
@kick_and_ban_all_members_botto your Telegram Group or Supergroup. - Promote the bot to Administrator with the Ban Users (
can_restrict_members) permission enabled. - Run
/startto verify permissions and/remove_all_members(or/purge) to start the background purge worker.
| Bot Command | Required Caller Role | Action |
|---|---|---|
/start | Any Member / Admin | Verifies bot installation and checks can_restrict_members privileges |
/remove_all_members | Group Owner / Administrator | Starts asynchronous MTProto purge of non-admin human members |
/purge | Group Owner / Administrator | Alias for /remove_all_members |
/help | Any Member / Admin | Displays setup checklist and immunity safeguard rules |
Security & Privilege Safeguards
- Strict Caller Authorization:
is_user_admin()validates the command sender's liveChatMemberstatus against Telegram servers before spawning the purge task. - Owner, Admin & Bot Immunity: Group creators, co-administrators, and bots are filtered out before
ban_chat_memberis invoked. - In-Memory MTProto Session: Pyrogram initializes with
in_memory=True, ensuring zero.sessionSQLite files persist on disk after the purge completes.
Get notified about updates to this tool
No spam. Just high-quality updates about new features and guides.
Related Tools
Related Posts
Learn how to detect Google AdSense on any website using Python. Covers fast HTML scanning with BeautifulSoup, ads.txt validation, and headless Playwright automation to bypass bot protection and lazy loading.
Learn how to easily download YouTube video thumbnails using Python's requests library, without the need for heavy dependencies like Pytube. This guide covers extracting video IDs from various YouTube URL formats and fetching the highest quality thumbnail available.
This piece will introduce Make (formerly Integromat) as a direct competitor to n8n, highlighting its visual, diagram-like workflow builder. The comparison will focus on key differences in pricing models, UI/UX, and advanced features like branching logic, which are a major selling point for Make. Examples will show how to build the same complex workflow in both n8n and Make to illustrate their unique approaches.