nikdoof.com

/posts/ 2023/fixing-mastodon-cached-media

Fixing Mastodon's Cached Media

Feb 26, 2023

#mastodon #ruby #fediverse 

Losing your cached media folder on Mastodon can be a pain. For days your instance will show broken images and issues to your end users. I operate a small instance at incognitus.net for a small community of friends, and recently I needed to move the storage from one NAS to another, to save the pain of the transfer speed I decided on nuking the cache and sorting it out after the fact. As it turns out this was a terrible mistake and not easy to remedy.

After trawling Github and some discussions around Mastodon, I came across a useful post by Tero Keski-Valkama detailing how to use the Rails console to run some fixes over your data. I noticed that some were a little heavy-handed and caused a lot of media to be re-downloaded, so I used by very basic Ruby knowledge to try and reduce the workload on the DB.

So here is the snippet I came up with:

MediaAttachment.cached.where.not(remote_url: '').each do |attachment|
  def save = false
  if attachment.file? && !attachment.file.exists?
    attachment.file.destroy 
    save = true
  end
  if attachment.thumbnail? && !attachment.thumbnail.exists?
    attachment.thumbnail.destroy
    save = true
  end
  if save
    puts attachment
    attachment.save
  end
end

This will check every cached media attachment, and if the file or thumbnail doesn’t exist it’ll clear them and save the record. The next time Mastodon looks at that MediaAttachment it should identify that the file needs re-downloading from the original source.

Hope this is useful for someone, it took about 5-10 minutes to run on my small instance with about 50GB of cached files (according to the database).

[Edit - 2023/02/07]

As it turns out, this still isn’t the magic bullet to resolve missing media, some items are still showing as dead images, but it did take care of the vast majority. I should spend more time investigating why they’ve been missed, but by that time my cronjob to cleanup remote media would of already nuked them.