I run my own Mastodon instance at isaacsu.com. (Not anymore) Over time, it accumulates avatar and header files of accounts that I do not care about in the live/public/system/accounts
directory, and it gets quite large.
Here’s a Rails console script that I use to purge them every now and again. There’s one method unused_accounts
that tries to assemble a whitelist of account_ids
that I’ve ever interacted with and returns a list of accounts that are not them.
1. Start by booting up Rails console for mastodon
cd ~/live
RAILS_ENV=production bundle exec rails console
2. Define the unused_accounts
method
Paste this code block into Rails console.
def unused_accounts
favourites = Favourite.all
.map{ |f| f.account_id }
.uniq
favourite_statuses = Favourite.all
.map{ |f| [f.status.account_id, f.status.in_reply_to_account_id] }
.flatten.uniq
follows = Follow.all
.map{ |f| [f.account_id, f.target_account_id] }
.flatten.uniq
notifications = Notification.all
.map{ |n| [n.account_id, n.from_account_id] }
.flatten.uniq
poll_votes = PollVote.all
.map{ |p| p.poll.account_id }
.flatten.uniq
activities = (favourites + favourite_statuses + follows + notifications + poll_votes)
.uniq
replied = Status.where(account_id: activities, reply: true)
.map{ |s| s.in_reply_to_account_id }
.uniq
reblogged = Status.where(account_id: (activities+replied).reject(&:nil?), reply: true)
.reject{ |s| s.reblog_of_id.nil? }
.map{ |s| Status.find(s.reblog_of_id).account_id }
.uniq
all = (activities + replied + reblogged).reject(&:nil?).uniq
Account.where.not(id: all)
end
3. Suspend them
Paste this code block into Rails console.
unused_accounts.each do |account|
puts "#{account.username} #{account.domain}"
SuspendAccountService.new.call(account, destroy: true)
end
A most recent run freed up 4GB
of disk space. bin/tootctl media remove --days=5
helped reclaim another ~3GB
on top of that.
Hope someone else finds this useful. I should note that this worked on Mastodon v2.9.2. There’s no guarantee that it will work in future or past versions.