Category Archives: Ubuntu

Mastodon cleanup scripts

Comments and improvement suggestions welcomed!

Anything to improve is good! I have been running this script with days 5. All of this may update frequently, so check back.

This script is adapted from Hosting your own Mastodon server.

I am currently running a 2 server cluster. The main server is Mastodon v4.1.6 on Ubuntu 20.04 LTS and the database server is PostgreSQL on Ubuntu 22.04 LTS. With ~25 users, disk space used averages around 50GB or less on the main server, and around 10GB or less on the database server.

This script is installed in /home/mastodon/maint/ on the main server. There is no configuration of the database server necessary for this script, nor any functional difference between an all in one Mastodon server and Mastodon with a separate database server.

cron

# Once a day cleanup Monday-Sunday 2:45AM CST (7:45 UTC)
45 7 * * * /home/mastodon/maint/cleanup-daily.sh > /home/mastodon/maint/cleanup-daily.log

cleanup-daily.sh

#!/bin/bash
PATH=$PATH:/home/mastodon/.rbenv/shims:/home/mastodon/.rbenv/bin:/usr/sbin:/usr/bin:/sbin:/bin

date

# This does the heavy lifting and removes locally cached copies of
# media attachments, avatars and headers from other servers
# By default it's set to delete media older than 7 days
# Adjust days with --days, eg --days 3 for a more aggressive setting.
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --days 5
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --prune-profiles --days 5
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --remove-headers --days 5

# New instead of above + --include-follows --dryrun --verbose ***
# RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --attachments --avatars --headers --days 5

# Remove local thumbnails for preview cards (the thumbnail when links are shared, etc)
# The default is 180 days and under 14 days is not recommended as they will not be refetched
RAILS_ENV=production /home/mastodon/live/bin/tootctl preview_cards remove --days 90

# Deletes remote accounts that never interacted with local accounts
# Removes many avatar and header images as part of this
RAILS_ENV=production /home/mastodon/live/bin/tootctl accounts prune

# Removes files that do not belong to existing media attachments
# For example if you attached a file to a post composer but never posted it
# This command and the one after it are slow to run, so I like to keep them for last
RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove-orphans

# Remove unreferenced statuses from the database, such as statuses that
# came from relays or from users who are no longer followed by any local accounts,
# and have not been replied to or otherwise interacted with (90 days default)
RAILS_ENV=production /home/mastodon/live/bin/tootctl statuses remove --days 90

date

*** Note it appears tootctl media remove is going to have an update to flags, re: Issue 23628

Thanks!
-Yehuda

Updated periodically – About TurtleIsland.social
Updated periodically – Migrating to TurtleIsland.social
Updated periodically – Introduction to Settler Colonialism
Updated periodically – Decolonization is not a metaphor
Updated periodically – Mvskoke History & Resources

Follow on Mastodon – TurtleIsland.social/@Yehuda

This page is subject to content updates/additions. If you think any content should be updated or added, please leave a private comment on Mastodon – TurtleIsland.social/@Yehuda.