Connect with us

Pay Per Code

Pay Per Code

Delete Your Tweets!

API

Delete Your Tweets!

By no means am I a seasoned veteran to Linux and Bash scripts but I do like programming and my passion for learning it certainly has a way of motivating me to do things like finding an efficient way to delete over 2,000 tweets.

Upon looking for a solution to delete all my tweets, I was saddened to discover that the only easy solution would be to use a third party app, which means handing over permissions to that app.

Additionally, deleting all those tweets, simply through the Twitter UI, would have cost me more time than to learn how to develop this app.

So, the happy medium? Program a script in Linux that does just that.

Now, after reading this post, you’ll be able to implement your own tweet deleter by using only a Linux terminal and a few command line utilities, namely, one to parse JSON, and another to manipulate text.

Be warned, though, I will skip over a lot of technical details by simply referring you to articles I found along my way to writing this script.

So, with that said, let’s begin.

First, on GitHub, there’s a command line utility called twurl. It’s easy to use and all the authentication is taken care of through a few simple steps; this will, easily, free the way to handling their REST API when making requests for data.

Twurl uses OAuth for authentication. Basically, a url is generated, you get a token and you paste it at the command line for later use when requesting data through Twitter’s REST API.

Twurl can be found here, as well as all the instructions to install it. I’m assuming you’ll figure that out on your own.

You can find additional instructions here, should you find yourself stuck.

After authentication is done, we also need a command line utility to parse JSON to a pretty print format so that the output that’s printed is easier on the eyes than just a blob of text.

In this demonstration, I will be using jq, where, if you don’t already have it on your Linux machine, you can sudo apt-get install jq.

Note: this is not a tutorial on how to use jq so should you find yourself interested in the intricacies of JSON parsing, then referencing the manual is as easy as typing man jq from the command line. Further, Stack Exchange is likely to have some examples and solutions to common problems.

The scripting is presented below:
#!/bin/bash
#**************************************COMMENTS**************************
#AUTHOR: Nikolaus Bertino
#the purpose of this script is to request an authenticated user's timeline,
#store that data into a variable, extract tweet ids from it, and then use those
#ids as a means to destroy the tweets by id, using the Twitter REST API.
counter=0
end=1
while [ $counter -lt $end ]; do
#request user timeline and store in variable to limit amount of requests performed
twitter_data=`twurl /1.1/statuses/user_timeline.json`
#parse string to convert to integer by removing quotes from beginning of string
#and end of string, and then store those values into an array
id_number_Array=(`echo $data | jq '.[] | .id_str' | sed -e 's/^"//' -e 's/"$//'`)
#print values before actually posting request to delete tweet
#to ensure url is correctly formatted
for i in "${id_number_Array[@]}";do
echo /1.1/statuses/destroy/$i.json;
done;
#actual twurl request to delete tweet, using number array
#to programmatically iterate through each id from user's timeline, using
#REST API to do so
#***************UNCOMMENT THE THREE LINES BELOW THIS LINE TO DELETE TWEETS****************
#for i in "${id_number_Array[@]}";
# do twurl -X POST echo /1.1/statuses/destroy/$i.json | jq '.';
#done;
((counter++))
done;

Note that the looping structure could be set up differently. For each $twitter_data that is stored in that variable, there are x number of tweet ids.

So, while the while loop gets a batch of data and then the for loop iterates through that data, a not-so-easy-to-determine-amount-of-times must go through the while loop in order to delete all tweets.

How I went about this was setting the $end value either very high till an error was produced or very low simply to see that tweets were being deleted. I’ll leave that decision up to you to solve in whatever manner you wish to implement.

Now that you have the script, happy deleting! I hope you find as much usefulness in this as I had…probably saved hours, or days of frustration from having to use the Twitter UI!

Moreover, with something like this, it can be refactored to suit other purposes, such as un-favoriting all the tweets you might have ‘liked’, but I’ll leave that part up to you to create.

Be sure to check out my other articles, you’re likely to find something useful in them.

Continue Reading
You may also like...
Comments

More in API

Instead of using ads, I am having you mine for crypto currency to support this site. Here are your stats:

You can start or stop at anytime but please wait for some accepted hashes before you do.

Contributors

Popular Posts

To Top