Automatically remove remote media attachments from Mastodon instance using systemd.

Create Ruby script that will remove cached media attachments.

$ tee /home/mastodon/clean_cached_media_attachments.rb <<EOF
#!/usr/bin/env ruby
require "/home/mastodon/live/config/environment"

print "Initial number of cached objects: #{MediaAttachment.cached.count}\n"
MediaAttachment.where.not(remote_url: '').where.not(file_file_name: nil).each { |f| f.file.destroy; f.save }
print "Final number of cached objects: #{MediaAttachment.cached.count}\n"
EOF

Ensure that executable bit is set.

$ chmod +x /home/mastodon/clean_cached_media_attachments.rb

Try it out.

mastodon@adventurousbeastie:~$ RAILS_ENV=production ruby /home/mastodon/clean_cached_media_attachments.rb
Initial number of cached objects: 278
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/921/828/282/265/097/original/baba0796ccadda8a.jpeg
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/921/828/282/265/097/small/baba0796ccadda8a.jpeg
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/924/398/421/725/289/original/4f5b57b3dfe733ec.jpeg
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/924/398/421/725/289/small/4f5b57b3dfe733ec.jpeg
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/924/398/445/304/505/original/105f1690e4a5f4d5.jpeg
[...]
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/935/243/344/841/024/small/bf6998d365b56c94.jpg
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/935/279/802/500/485/original/491fc7ba5e91e16b.png
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/935/279/802/500/485/small/491fc7ba5e91e16b.png
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/935/282/609/708/532/original/72500079e2a70374.jpg
[paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/935/282/609/708/532/small/72500079e2a70374.jpg
Final number of cached objects: 0

Create service that will execute this Ruby script.

$ sudo tee /etc/systemd/system/mastodon-remove-cached-media-attachments.service <<EOF
[Unit]
Description=Mastodon - remove cached media attachments
Wants=mastodon-remove-cached-media-attachments.timer

[Service]
Type=oneshot

User=mastodon
Group=mastodon

Environment="RAILS_ENV=production"
Environment="PATH=/home/mastodon/.rbenv/shims:/home/mastodon/.rbenv/bin:/bin"
WorkingDirectory=/home/mastodon/
ExecStart=/home/mastodon/clean_cached_media_attachments.rb

[Install]
WantedBy=multi-user.target
EOF

Execute it everyday at 06:00 in the morning.

$ sudo tee /etc/systemd/system/mastodon-remove-cached-media-attachments.timer <<EOF
[Unit]
Description=Schedule a media remove every week

[Timer]
Persistent=true
OnCalendar=*-*-* 06:00:00
Unit=mastodon-remove-cached-media-attachments.service

[Install]
WantedBy=timers.target
EOF

Reload systemd configuration.

$ systemctl daemon-reload

Try out created service.

$ sudo systemctl start mastodon-remove-cached-media-attachments.service
$ sudo systemctl status mastodon-remove-cached-media-attachments.service
○ mastodon-remove-cached-media-attachments.service - Mastodon - remove cached media attachments
     Loaded: loaded (/etc/systemd/system/mastodon-remove-cached-media-attachments.service; disabled; vendor preset: enabled)
     Active: inactive (dead) since Tue 2023-08-22 23:17:12 CEST; 3s ago
TriggeredBy: ● mastodon-remove-cached-media-attachments.timer
    Process: 3932564 ExecStart=/home/mastodon/clean_cached_media_attachments.rb (code=exited, status=0/SUCCESS)
   Main PID: 3932564 (code=exited, status=0/SUCCESS)
        CPU: 6.190s

Aug 22 23:17:12 adventurousbeastie.eu clean_cached_media_attachments.rb[3932564]: [paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/932/005/171/145/431/original/55b8c6a0>
Aug 22 23:17:12 adventurousbeastie.eu clean_cached_media_attachments.rb[3932564]: [paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/932/005/171/145/431/small/55b8c6a0527>
Aug 22 23:17:12 adventurousbeastie.eu clean_cached_media_attachments.rb[3932564]: [paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/934/854/614/076/010/original/952fdc46>
Aug 22 23:17:12 adventurousbeastie.eu clean_cached_media_attachments.rb[3932564]: [paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/934/854/614/076/010/small/952fdc46661>
Aug 22 23:17:12 adventurousbeastie.eu clean_cached_media_attachments.rb[3932564]: [paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/935/316/032/683/874/original/ebb3ec67>
Aug 22 23:17:12 adventurousbeastie.eu clean_cached_media_attachments.rb[3932564]: [paperclip] deleting /home/mastodon/live/public/system/cache/media_attachments/files/110/935/316/032/683/874/small/ebb3ec672b8>
Aug 22 23:17:12 adventurousbeastie.eu clean_cached_media_attachments.rb[3932564]: Final number of cached objects: 0
Aug 22 23:17:12 adventurousbeastie.eu systemd[1]: mastodon-remove-cached-media-attachments.service: Deactivated successfully.
Aug 22 23:17:12 adventurousbeastie.eu systemd[1]: Finished Mastodon - remove cached media attachments.
Aug 22 23:17:12 adventurousbeastie.eu systemd[1]: mastodon-remove-cached-media-attachments.service: Consumed 6.190s CPU time.

Enable timer.

$ sudo systemctl enable --now mastodon-remove-cached-media-attachments.timer
Created symlink /etc/systemd/system/timers.target.wants/mastodon-remove-cached-media-attachments.timer → /etc/systemd/system/mastodon-remove-cached-media-attachments.timer.

List timers.

$ systemctl list-timers
NEXT                         LEFT          LAST                         PASSED        UNIT                                           ACTIVATES
Wed 2023-08-23 00:00:00 CEST 43min left    Tue 2023-08-22 00:00:02 CEST 23h ago       dpkg-db-backup.timer                           dpkg-db-backup.service
Wed 2023-08-23 00:00:00 CEST 43min left    Tue 2023-08-22 00:00:02 CEST 23h ago       logrotate.timer                                logrotate.service
Wed 2023-08-23 05:00:00 CEST 5h 43min left Tue 2023-08-22 05:00:02 CEST 18h ago       mastodon-media-remove.timer                    mastodon-media-remove.service
Wed 2023-08-23 05:30:00 CEST 6h left       Tue 2023-08-22 05:30:02 CEST 17h ago       mastodon-preview_cards-remove.timer            mastodon-preview_cards-remove.service
Wed 2023-08-23 06:00:00 CEST 6h left       n/a                          n/a           mastodon-remove-cached-media-attachments.timermastodon-remove-cached-media-attachments.service
Wed 2023-08-23 06:00:00 CEST 6h left       Tue 2023-08-22 06:00:02 CEST 17h ago       mastodon-search-deploy.timer                   mastodon-search-deploy.service
Wed 2023-08-23 06:07:25 CEST 6h left       Tue 2023-08-22 06:21:02 CEST 16h ago       apt-daily-upgrade.timer                        apt-daily-upgrade.service
Wed 2023-08-23 06:17:36 CEST 7h left       Tue 2023-08-22 15:09:02 CEST 8h ago        motd-news.timer                                motd-news.service
Wed 2023-08-23 11:04:59 CEST 11h left      Tue 2023-08-22 21:29:41 CEST 1h 46min ago  apt-daily.timer                                apt-daily.service
Wed 2023-08-23 15:29:47 CEST 16h left      Tue 2023-08-22 15:29:47 CEST 7h ago        systemd-tmpfiles-clean.timer                   systemd-tmpfiles-clean.service
Sun 2023-08-27 03:10:01 CEST 4 days left   Sun 2023-08-20 03:10:13 CEST 2 days ago    e2scrub_all.timer                              e2scrub_all.service
Mon 2023-08-28 01:21:10 CEST 5 days left   Mon 2023-08-21 00:45:11 CEST 1 day 22h ago fstrim.timer                                   fstrim.service

12 timers listed.
Pass --all to see loaded but inactive timers, too.

Beware, some images on your timeline can be missing.

ko-fi