刪除 Twitter 所有 Tweets (Python)

之前就一直很想整理那個亂亂的 Twitter 帳號,苦無方法一次可以把全部以往訊息刪除的工具!
因為以前都亂連動一些有的沒的社群網路,整個舊有點髒髒,上網搜尋到幾個輔助網站也沒啥鳥用!剛好看到這位 Dave Jeffery 寫的小巧 Python Script 來達成任務

# -*- coding: utf-8 -*-
"""
This script will delete all of the tweets in the specified account.
You may need to hit the "more" button on the bottom of your twitter profile
page every now and then as the script runs, this is due to a bug in twitter.
You will need to get a consumer key and consumer secret token to use this
script, you can do so by registering a twitter application at https://dev.twitter.com/apps
@requirements: Python 2.5+, Tweepy (http://pypi.python.org/pypi/tweepy/1.7.1)
@author: Dave Jeffery
"""
import tweepy
CONSUMER_KEY = 'XXX'
CONSUMER_SECRET = 'XXX'
def oauth_login(consumer_key, consumer_secret):
"""Authenticate with twitter using OAuth"""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth_url = auth.get_authorization_url()
verify_code = raw_input("Authenticate at %s and then enter you verification code here > " % auth_url)
auth.get_access_token(verify_code)
return tweepy.API(auth)
def batch_delete(api):
print "You are about to Delete all tweets from the account @%s." % api.verify_credentials().screen_name
print "Does this sound ok? There is no undo! Type yes to carry out this action."
do_delete = raw_input("> ")
if do_delete.lower() == 'yes':
for status in tweepy.Cursor(api.user_timeline).items():
try:
api.destroy_status(status.id)
print "Deleted:", status.id
except:
print "Failed to delete:", status.id
if __name__ == "__main__":
api = oauth_login(CONSUMER_KEY, CONSUMER_SECRET)
print "Authenticated as: %s" % api.me().screen_name
batch_delete(api)

事前準備

  1. Twitter App 你可以到這邊申請
  2. Python 2.5+
  3. Tweepy sudo pip install tweepy
  4. Oauthlib sudo pip oauthlib
  5. 上面那段 Script git clone https://gist.github.com/113241.git

動手開幹

接下來把 Script 中的 CONSUMER_KEYCONSUMER_SECRET 換成你剛剛申請 Twitter APP 的。然後執行 python delete_all_tweets.py

他就會出現提示Authenticate at XXX(你的帳號) and then enter you verification code here > 後面會接 oauth 的網址,點下去按授權會拿到一組 verify code,貼上來按 Enter

接著他會確認你是否真的要刪除,輸入 yes (大小寫沒差)。之後就會跳出

Deleted: XXX
Deleted: XXX
Deleted: XXX
Deleted: XXX
Deleted: XXX

等待他執行完畢後,你的帳號就乾乾淨淨囉!