Skip to main content

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.

Kick and Ban All Members Bot Interface


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​

  1. Add @kick_and_ban_all_members_bot to your Telegram Group or Supergroup.
  2. Promote the bot to Administrator with the Ban Users (can_restrict_members) permission enabled.
  3. Run /start to verify permissions and /remove_all_members (or /purge) to start the background purge worker.
Bot CommandRequired Caller RoleAction
/startAny Member / AdminVerifies bot installation and checks can_restrict_members privileges
/remove_all_membersGroup Owner / AdministratorStarts asynchronous MTProto purge of non-admin human members
/purgeGroup Owner / AdministratorAlias for /remove_all_members
/helpAny Member / AdminDisplays setup checklist and immunity safeguard rules

Security & Privilege Safeguards​

  • Strict Caller Authorization: is_user_admin() validates the command sender's live ChatMember status against Telegram servers before spawning the purge task.
  • Owner, Admin & Bot Immunity: Group creators, co-administrators, and bots are filtered out before ban_chat_member is invoked.
  • In-Memory MTProto Session: Pyrogram initializes with in_memory=True, ensuring zero .session SQLite 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.