How can I make my Lemmy server delete cached images?
The storage usage is at 340GB currently, which is a lot and it's rapidly increasing. I use Backblaze B2 for my storage. Here is my docker compose file:
x-logging: &default-logging
driver: "json-file"
options:
max-size: "50m"
max-file: "4"
Lemmy doesn't so much cache the images so much as "store them forever".
Regular post thumbnails are just "internal" images. I'm not sure if newer Lemmy versions log these internally like user uploads are, but assuming not, there's no direct way to deal with them.
Pict-rs keeps a true cache of image variants (alternate versions of images that were requested at different resolutions / formats). Those can be cleared programmatically:
Where your pict-rs API port is exposed to localhost on port 8080. Also note that those variants will be re-created on demand.
One easy-ish way to get rid of images is to select from the post table for any thumbnail_url that starts with your instance. e.g. https://your-instance/pictrs/image/%s. You can filter that by published date if you only want to wipe out thumbnails for posts older than a year, for example.
With that list, you can strip off the filename/alias (the uuid and extension) and pass each alias filename to a script that tells pict-rs to delete it (you need at least pict-rs 0.5 for this to work):
Below 0.5, the only way to delete images fro pict-rs without the delete token was to use the purge endpoint. But that deletes all aliases and not just the one you want to delete since it does de-duplication under the hood.
If you want to get fancy so that image posts still render correctly, when building your list of thumbnails to delete, you can check if the value of the url column is an image. If it is, grab the current thumbnail image to pass to the delete function and then update the thumbnail URL value to that of the post URL.