Created
June 14, 2025 23:26
-
-
Save ivmirx/fecb7075dd093386b316bf042943a11d to your computer and use it in GitHub Desktop.
Delete your messages in Telegram chats before a specific date (with deletion from another person's history)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script deletes your own messages older than a cutoff date in a 1:1 chat | |
# They're also deleted from another person's history | |
# Create an app at https://my.telegram.org | |
# Save the `api_id` and `api_hash` from there | |
# Launching: | |
# python3 -m venv pyrogram_env | |
# source pyrogram_env/bin/activate | |
# pip install pyrogram tgcrypto | |
# python wipergram.py | |
# On first run it asks for the phone number (with `+`) | |
# Then the `.session` file stores your login and will be reused on future runs | |
from pyrogram import Client | |
from datetime import datetime, timedelta | |
import logging | |
import time | |
# Replace with your values | |
api_id = YOUR_APP_ID | |
api_hash = "YOUR_API_HASH" | |
target_chat = "PERSON_USERNAME" # username without `@` or numeric ID | |
cutoff_date = datetime(2023, 12, 31) # delete messages older than this | |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s") | |
app = Client("my_account", api_id=api_id, api_hash=api_hash) | |
with app: | |
count = 0 | |
skipped = 0 | |
total = 0 | |
# this may take a while because Telegram API can't jump to a specific date | |
# so the script walks backwards through all the messages | |
for msg in app.get_chat_history(target_chat): | |
total += 1 | |
if total % 100 == 0: | |
logging.info(f"Traversing: currently at msg {msg.id} dated {msg.date}") | |
if msg.date >= cutoff_date: | |
continue # too new, skip | |
if msg.from_user and msg.from_user.is_self and not msg.service: | |
try: | |
preview = (msg.text or msg.caption or "[media]").replace('\n', ' ')[:60] | |
app.delete_messages(target_chat, msg.id, revoke=True) | |
logging.info(f"Deleted msg {msg.id} @ {msg.date}: '{preview}'") | |
count += 1 | |
time.sleep(0.2) # throttling deletions, so Telegram's API is happy | |
except Exception as e: | |
logging.error(f"Failed to delete msg {msg.id}: {e}") | |
else: | |
skipped += 1 | |
logging.info(f"Done. Deleted: {count}, Skipped: {skipped}, Traversed: {total}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment