Improving Mastodon’s disk usage

Mastodon's built-in CLI gives you the availability to clean attachments and previews from remote accounts, purging the disk cache. This is fantastic and you couldn't possible survive with out it.

My current crontab that runs every 3 hours:

0 */3 * * * /bin/bash /home/mastodon/purge-media.shCode language: Bash (bash)

As of Mastodon 4.1.0, we have new available commands. Here's the content of my script:


# Prune remote accounts that never interacted with a local user
RAILS_ENV=production /home/mastodon/live/bin/tootctl accounts prune;

# Remove remote statuses that local users never interacted with older than 4 days
RAILS_ENV=production /home/mastodon/live/bin/tootctl statuses remove --days 4;

# Remove media attachments older than 4 days
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --days 4;

# Remove all headers (including people I follow)
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --remove-headers --include-follows --days 0;

# Remove link previews older than 4 days
RAILS_ENV=production /home/mastodon/live/bin/tootctl preview_cards remove --days 4;

# Remove files not linked to any post
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove-orphans;Code language: PHP (php)

⚠️ If you've never run these commands before I'd suggest run them one by one (not in a cronjob) as they might take several hours (or days) to run each. The size of the cached media and database will depend on how many people you follow, how many are on your instance, how many relays you have added to your instance, etc.

Having like 10 relays added to my single-user instance with the bash script above I'm around 30Gb in my Object Storage S3.

Do you have any other tips on how to keep a Mastodon instance lean?

πŸ‘‹ Don't miss the follow up post: Scaling Mastodon: moving media assets to Object Storage


  1. @wild1145 I think it could have been something with IPv6 I’ve compared my other domains and .dev had it set up at the Linode Domain dashboard, for some reason.I’ve removed the extra configuration (which I don’t remember creating) for IPv6 and now they look the same, see if that solves it after propagation 🀞 Thank you again for the feedback πŸ’œ

  2. @ricard I do not have any cronjobs, I have delayed it for so long, obviously too long I actually have that page open, going to set up a cronjob soon as soon as I get the initial prunes done (need some space before making it automatic).Hetzner 1TB storage costs € 3.20 per month. It’s the cheapest I know. And really reliable too.

  3. @ricard @anlomedad While it takes a while to do so you might not want to run it every night (as well as some providers charging for the api calls to check all the files), I didn’t see a command in there to remove orphans ( )tootctl media remove-orphansIt will go through for media files that aren’t connected to stuff anymore, just taking up space.
    Using the admin CLI – Mastodon documentation

  4. @mondanzo It’s different. The cache retention deletes everything (posts you’ve liked, bookmarked, etc).There’s a PR to amend the text so it’s easier to understand.The CLI commands are way more refined and only delete stuff you haven’t interacted with, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *