Keeping My Mastodon Instance Lean

A year ago, I ran into an issue where my Mastodon instance locked up. Everything appeared to be working, however, posts weren't updating and I was unable to post myself. It was strange as no settings had been changed and there hadn't be an outage.

To the logs I go...

It appears that I had run out of space. This was inevitable as, I run my Mastodon instance on a self hosted kubernetes cluster with tuned, locked resources. So, how do I fix it? To the internet I go.

I came across the blog post by Ricard entitled Improving Mastodon’s disk usage. A great description is provided as to what each command does. Essentially, that post is the foundation to what is shared here.

Similarly, I will be setting up a cronjob. I plan to run a monthly clean up job, removing anything older than two weeks.

This is what the script looks like. I shall call it mastodon-cleaner.sh.


#!/bin/bash

# Run the command and assign the output to a variable
output=$(kubectl get pods -o wide | grep mastodon)

# Use awk to get the first word
instance=$(echo $output | awk '{print $1}')

# Prune remote accounts that never interacted with a local user
kubectl exec -it $instance -- tootctl accounts prune

# Remove remote statuses that local users never interacted with older than 14 days
kubectl exec -it $instance -- tootctl statuses remove --days 14;

# Remove media attachments older than 14 days
kubectl exec -it $instance -- tootctl media remove --days 14;

# Remove all headers (including people I follow)
kubectl exec -it $instance -- tootctl media remove --remove-headers --include-follows --days 0;

# Remove link previews older than 14 days
kubectl exec -it $instance -- tootctl preview_cards remove --days 14;

# Remove files not linked to any post
kubectl exec -it $instance -- tootctl media remove-orphans;
mastodon-cleaner.sh

You can see the commands are different and so is the "--days" parameter. As stated earlier, I only want to keep two weeks of history. Also, this being a kubernetes instance, the commands are going to be specific to the pod. That is where the first two commands come in.

Next, the script will be run on the control-plane node of the kubernetes cluster.


0 3 1 * * /bin/bash /home/{user}/mastodon-cleaner.sh

This will be run the first day of every month at 3am.

The post doesn't discuss the permissions associated with running kubectl, kubernetes or the cron user specific permissions, however, I hope it gives a basic perspective into how solutions to other's problems can be added to solve your own (Don't forget to give them credit for their work!). Happy Hosting!