iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💡

Deleting all past Mastodon toots

に公開

I decided to delete all my past toots because I wanted to restart a Mastodon account I made a long time ago. I'm recording the method here as a memo.

First, since we'll be calling the API, an access token is required. That said, it's easy to obtain one simply by creating an application in Mastodon's Development settings. The default permissions (read/write/follow) are sufficient.

I'll write and run a simple Ruby script. First, place the following three files locally.

Gemfile
 # frozen_string_literal: true
 
 source "https://rubygems.org"
 
 # NOTE: The official mastodon-api gem does not implement an endpoint to retrieve your own toots, so I added it myself
 gem 'mastodon-api', require: 'mastodon', git: "https://github.com/YuheiNakasaka/mastodon-api", branch: "feature/get-accounts-statues"
 gem 'dotenv', require: false
.env
 MASTODON_BASE_URL=https://mstdn.jp
 MASTODON_BEARER_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 MASTODON_MY_ID=123456
 TOTAL_STATUS_COUNT=1000
delete_all_statuses.rb
 require "mastodon"
 require "dotenv"
 
 Dotenv.load
 
 client = Mastodon::REST::Client.new(base_url: ENV["MASTODON_BASE_URL"], bearer_token: ENV["MASTODON_BEARER_TOKEN"])
 
 i = 0
 total = ENV["TOTAL_STATUS_COUNT"].to_i / 40
 while i <= total
   statuses = client.get_account_statuses(ENV["MASTODON_MY_ID"], {limit: 40})
   statuses.each do |status|
     begin
       id = status.attributes["id"]
       created_at = status.attributes["created_at"]
       puts("Deleting status #{id} created at #{created_at}.")
       client.destroy_status(id)
       sleep(61)
     rescue => e
       puts(e)
     end
   end
 
   if statuses.size == 0
     break
   end
 
   i += 1
 end

First, run bundle install to install the gems.

Then, set the necessary credentials in the .env file. Set your account's base URL in MASTODON_BASE_URL. Set the access token you just obtained in MASTODON_BEARER_TOKEN. Set your account ID in MASTODON_MY_ID. Set the number of your posts in TOTAL_STATUS_COUNT.

By the way, if you don't know your account ID, it's a good idea to call the verify_credentials API. You can find it out quickly by doing something like the following:

 client = Mastodon::REST::Client.new(base_url: ENV["MASTODON_BASE_URL"], bearer_token: ENV["MASTODON_BEARER_TOKEN"])
 p client.verify_credentials

Now, just run the script and the deletion process will start.

 $ bundle exec ruby delete_all_statuses.rb

Due to the API rate limit, you can only perform one deletion per minute, so it will take the number of minutes specified in TOTAL_STATUS_COUNT. For example, deleting 1,440 posts would take 1,440 minutes (≈ 1 day). There's nothing to do but be patient...

In my case, I had 8,000 or so toots because I had enabled automatic cross-posting from Twitter in the past, so it took about 5 to 6 days to delete them all...

My account is as follows, feel free to follow me.

Resources

Discussion