DELETE YOUR TWEETS AND LIKES WITH PYTHON
If you want to delete all your tweets and likes with one shot, you can use this python script.
you need to find (or create) you api keys and secrets from https://developer.x.com/en/portal/dashboard page.
import json
import time
import tweepy
import os
from datetime import datetime
def load_tweet_ids_from_archive(file_path):
"""Load tweet IDs from Twitter archive files (tweets.js or like.js)"""
tweet_ids = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
# Twitter archive files have some JavaScript at the start
content = f.read().replace('window.YTD.tweets.part0 = ', '')
data = json.loads(content)
for tweet in data:
tweet_id = tweet['tweet']['id_str']
tweet_ids.append(tweet_id)
except Exception as e:
print(f"Error loading {file_path}: {str(e)}")
return tweet_ids
def delete_tweets(api, tweet_ids, batch_size=100, delay_seconds=2):
"""Delete tweets in batches with delay to avoid rate limits"""
deleted_count = 0
failed_count = 0
for i in range(0, len(tweet_ids), batch_size):
batch = tweet_ids[i:i + batch_size]
for tweet_id in batch:
try:
api.destroy_status(tweet_id)
print(f"Deleted tweet {tweet_id}")
deleted_count += 1
time.sleep(delay_seconds)
except tweepy.errors.TweepyException as e:
if 'No status found with that ID' in str(e):
print(f"Tweet {tweet_id} already deleted or not found")
else:
print(f"Failed to delete tweet {tweet_id}: {str(e)}")
failed_count += 1
print(f"Progress: {deleted_count} deleted, {failed_count} failed")
return deleted_count, failed_count
def main():
# Load Twitter API credentials from environment variables
consumer_key = os.getenv('TWITTER_API_KEY')
consumer_secret = os.getenv('TWITTER_API_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
if not all([consumer_key, consumer_secret, access_token, access_token_secret]):
print("Please set all required Twitter API environment variables:")
print("TWITTER_API_KEY, TWITTER_API_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET")
return
# Authenticate with Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
# Verify credentials
try:
user = api.verify_credentials()
print(f"Authenticated as @{user.screen_name}")
except tweepy.errors.TweepyException as e:
print("Authentication failed:", str(e))
return
# Load tweet IDs from archive files
tweets_file = 'tweets.js' # Path to your tweets.js file
likes_file = 'like.js' # Path to your like.js file
if not os.path.exists(tweets_file):
print(f"Error: {tweets_file} not found in current directory")
return
print("Loading tweet IDs from archive files...")
tweet_ids = load_tweet_ids_from_archive(tweets_file)
# Optional: Also delete liked tweets (uncomment if desired)
# like_ids = load_tweet_ids_from_archive(likes_file)
# tweet_ids.extend(like_ids)
if not tweet_ids:
print("No tweet IDs found in archive files")
return
print(f"Found {len(tweet_ids)} tweets to delete")
# Confirm before deletion
confirm = input(f"Do you want to delete {len(tweet_ids)} tweets? (yes/no): ").strip().lower()
if confirm != 'yes':
print("Deletion cancelled")
return
# Delete tweets
print("Starting deletion process...")
deleted, failed = delete_tweets(api, tweet_ids)
print("\nDeletion complete!")
print(f"Successfully deleted: {deleted}")
print(f"Failed to delete: {failed}")
if __name__ == '__main__':
main()