Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tweet delete script
require "rubygems"
require "twitter"
require "json"
# things you must configure
TWITTER_USER = "your_username"
MAX_AGE_IN_DAYS = 1 # anything older than this is deleted
# get these from dev.twitter.com
CONSUMER_KEY = "your_consumer_key"
CONSUMER_SECRET = "your_consumer_secret"
OAUTH_TOKEN = "your_oauth_token"
OAUTH_TOKEN_SECRET = "your_oauth_secret"
# any special favorites?
IDS_TO_SAVE_FOREVER = [369855688836538368] # my panini tweet
### you shouldn't have to change anything below this line ###
MAX_AGE_IN_SECONDS = MAX_AGE_IN_DAYS*24*60*60
NOW_IN_SECONDS = Time.now
TWEETS_PER_REQUEST = 200
### A METHOD ###
def delete_from_twitter(tweet, client)
begin
client.destroy_status(tweet.id)
rescue StandardError => e
puts e.inspect
puts "Error deleting #{tweet.id}; exiting"
exit
else
puts "Deleted #{tweet.id}"
end
end
### WE BEGIN ###
client = Twitter::REST::Client.new do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.access_token = OAUTH_TOKEN
config.access_token_secret = OAUTH_TOKEN_SECRET
end
puts ""
puts "What's that sound...?"
puts ""
tweets = []
got_tweets = true
oldest_tweet_id = 9000000000000000000
while got_tweets do
begin
new_tweets = client.user_timeline(TWITTER_USER, {:count => TWEETS_PER_REQUEST,
:max_id => oldest_tweet_id,
:include_entities => false,
:include_rts => true})
if (new_tweets.length > 0) then
puts "Got #{new_tweets.length} more tweets, latest is #{new_tweets.last.id}"
oldest_tweet_id = new_tweets.last.id - 1
tweets += new_tweets
else
got_tweets = false
end
rescue Twitter::Error::TooManyRequests => e
puts "Hit the rate limit; pausing for #{e.rate_limit.reset_in} seconds"
sleep e.rate_limit.reset_in
retry
rescue StandardError => e
puts e.inspect
exit
end
end
puts ""
puts "Got #{tweets.length} tweets total"
puts ""
tweets.each do |tweet|
begin
tweet_age = NOW_IN_SECONDS - tweet.created_at
tweet_age_in_days = (tweet_age/(24*60*60)).round
if (tweet_age < MAX_AGE_IN_SECONDS) then
puts "Ignored a tweet #{tweet_age_in_days} days old"
elsif IDS_TO_SAVE_FOREVER.include?(tweet.id) then
puts "Ignored a tweet that is to be saved forever"
else
puts "Deleting a tweet #{tweet_age_in_days} days old"
delete_from_twitter(tweet, client)
end
puts " #{tweet.text}"
puts ""
rescue Twitter::Error::TooManyRequests => e
puts "Hit the rate limit; pausing for #{e.rate_limit.reset_in} seconds"
sleep e.rate_limit.reset_in
retry
rescue StandardError => e
puts e.inspect
exit
end
end
@robinsloan

This comment has been minimized.

Copy link
Owner Author

commented Oct 21, 2016

Note: updated in October 2016 with a leaner, meaner approach.

@notDavid

This comment has been minimized.

Copy link

commented Jan 31, 2017

Thanks for this. Would be cool if it could delete likes also :-)

@robinsloan

This comment has been minimized.

Copy link
Owner Author

commented Feb 16, 2017

@notDavid I've got a script for that, too! Here you go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.