Twitter is an excellent tool for the troll armies out there, for endless and pointless entrenched discussion, for the digital archaeologist out there who wants to end your politics career, for wasting time and, last but not least, for discovering the eventual and rare link worth reading and for following real-time events (when there is actual, valuable information available, which is not usually the case).

I have a Twitter account since around 2008, and I have come to the conclusion that any tweet more than one month old is close to worthless. If the purpose of this is to have flash conversations, let's keep it that way: no tweet should be alive longer than the time it needs to reach its audience. And then it's time to put them down.

In this article I provide two Python scripts: one to mass-delete old tweets using the Twitter archive as the source, and another one to periodically trim your timeline to the size of your choice. The usual caveats apply: read the code and make sure you understand what it's doing, please do some tests before running it unattended, all under your responsibility, this might break your coffee machine, blah, blah.

So, without further ado:

  1. Download the code.

  2. Register the application on this Twitter developer page.

  3. Copy config-sample.py to config.py and edit the necessary variables. This includes the auth tokens that you will obtain from the page above and the number of tweets you want to have at any given point in time.

  4. It turns out it is very tricky to obtain more than 3200 tweets for a given user. So, in order to batch-delete everything from the beginning, we are going to download the very handy Twitter archive. Go to your settings page and click on Your Twitter archive. Follow the instructions, download the .zip file and then copy the tweets.csv file to the folder containing the code.

  5. Run $ python delete_from_archive.py. This will delete all your tweets since the beginning of time and will leave only the most recent n_keep alive (you set this up in your config.py file).

  6. Every day, or hour, or whatever works for you, run delete_tweets.py. This will trim your timeline to the desired size.

And that's it. As long as you keep running the script from point 6, everything will be kept clean and tidy. It should be very easy to modify the code so that, instead of keeping the last N tweets, it keeps the ones from the last D days. I have no time to do this, but if you feel so inclined just take the code and help yourself.