Solve all your social media problems with this one weird trick

Everyone on Earth will sooner or later be fired because of a bad tweet they did. We could stop that by obeying the golden rule of Twitter1Who was it who first formulated the golden rule (“Never tweet”)? As far as I can tell, it just emerged from the collective unconscious somehow., but something about the app means that it is physically impossible to do so.

It is, however, possible to delay. By regularly deleting tweets you can protect yourself for a time being, just as carefully washing your hands will keep you alive during a viral epidemic until the day you’re hit by a bus.

There are third-party services that will do that for you.2Why isn’t this built in to Twitter itself? Good question. This analysis is still true, almost five years later. But Twitter’s auth setup means that you have to give them permission to have total control over all aspects of your account, which, like, yikes! So I decided to figure out how to do it myself in a bash one-liner for safety, and because it’s fun.

First caveat

The Twitter API uses OAuth, which means that there has to be a complicated back-and-forth between you and the server to establish your bona fides. It’s theoretically possible to do that in pure bash but it’s far easier to install a client program to handle it. Similarly, the API output is in the form of JSON, so you could parse that with Awk or whatever but the most straightforward solution is to use jq.

Here are the programs you’ll need to have installed. On macOS, if you have homebrew, you can do brew install X for each program X. On Linux it’s a matter of sudo apt-get install X or equivalent for your distro. If you’re a Windows user, there’s one simple prerequisite you need to do first.

(Some Linux distros come with a built-in version of parallel that isn’t compatible; it’s worth installing the GNU version on top.)

Second caveat

Twitter’s API only lets you access the most recent 1500 tweets you’ve done. To polish off your complete history, you need to use EU data-access rules to download your data, then apply the same process as described below. One catch is that they give you your tweets in JavaScript instead of JSON format, but you can easily edit the file to make it right. Just look for the file called tweet.js and remove the window.YTD.tweet.part0 = bit at the start. Then a cat ..../tweet.js | can replace the first part of the recipe below.


You’ll need to get Twitter to give you an access key. That’s a matter of going to their dev site and clicking Create An App.

The company have recently tightened up their Developer access, and now you have to write an essay explaining why you want it. I recommend clicking the button that says you want to “Explore the API” on the first screen; then on the second screen, put every radio button to No, and submit this description of how you will use Twitter data:

I am planning to explore how to automate personal tweet-deletion for a single user account. Under no circumstances do I intend to enable this app for other users. Under no circumstances do I intend to use this app for anything other than automated deletion of my own tweets.

Once you’ve confirmed your email, you will instantly be able to Create An App.

Under App Details, click in the App Name box and mash the keyboard (app names must be globally unique); set the app URL to; and in the Tell Us How This App Will Be Used box you can put this:

This app is only for my personal use. It will enable me to automate the deletion of my tweets. No other users will be allowed access.

Hey presto, you’re ready! Click over to the Keys and tokens tab. Open up a bash window (on both macOS and Linux that means starting the Terminal app) and do

$ twurl authorize --consumer-key put-the-"API-key"-gibberish-here --consumer-secret put-the-"API-secret-key"-gibberish here

Let’s do this

At last, here is the single line of bash which will make all your wildest dreams come true:

$ twurl "/1.1/statuses/user_timeline.json?screen_name=YOUR_TWITTER_HANDLE&count=200&max_id=$(twurl '/1.1/statuses/user_timeline.json?screen_name=YOUR_TWITTER_HANDLE&count=200&include_rts=1' | jq -r '.[9].id_str')&include_rts=1" | jq -r '.[].id_str' | parallel -j 10 -a - twurl -X POST /1.1/statuses/destroy/{1}.json > /dev/null

Obviously the YOUR_TWITTER_HANDLE bit there should be replaced by your own twitter handle, without the at sign.

Here’s how it works. First, use twurl to request the user_timeline. Nested in the request via a bash $(…) is another call to the same API so that you get the ID of your 10th most recent tweet.

That output is sent to jq with --compact-output and --raw-output options, abbreviated. The main argument to the jq call, '.[]|.id_str', uses its cryptic syntax to say “From the root level, scan through the array and extract every item’s id_str“. The '.[9]|.id_str' version inside the brackets extracts only the tenth result (labelled 9 because jq counts from zero).3A previous version used bash’s head and tail to find the tenth item, but this is more elegant. Thank you to HackerNews reader Pirate-of-SV for suggesting this improvement!

Those ID strings are sent to parallel, with a -j (jobs) of 10 because the main bottleneck is waiting for the Twitter server to respond, and -a - causing it to read from stdin, i.e., from the output of jq.

The parallel task is to send a POST request to the destroy API. The {1} bit is substituted with the id_str by parallel, and we pipe all the output to /dev/null because otherwise your terminal fills up with confirmation echoes of the original tweets, which gets old fast.


If you’ve found this article useful, please tweet about it. Feedback or suggestions? Send them to me on Twitter. Tweet through it. Never stop tweeting.