Skip to content

Instantly share code, notes, and snippets.

@Nutomic
Last active March 20, 2024 10:00
Show Gist options
  • Save Nutomic/611ac78a889c90f8c8e00207af73fa3c to your computer and use it in GitHub Desktop.
Save Nutomic/611ac78a889c90f8c8e00207af73fa3c to your computer and use it in GitHub Desktop.
lemmy-delete-old-post-thumbnails.bash
# Post thumbnails can take up a lot of space in Lemmy as they dont have a size
# limitation. This script helps with cleanup by reading old thumbnails from
# the db, then deleting them from pictrs.
#
# WARNING: Completely untested, use at your own risk!
#!/bin/bash
set -e
# pictrs api key, should be defined in docker-compose.yml
PICTRS_API_KEY="123"
# domain of your instance with leading https
MY_INSTANCE="https://lemmy.ml/"
# temporary file to store post thumbnail urls which should be deleted
THUMBNAILS_LIST="thumbnails.txt"
# regex to extract image alias (filename) from url
REGEX="^(.*)/pictrs/image/([a-z0-9-]+\.[a-z]+)$"
# add curl into pictrs container to call internal endpoints
docker-compose exec -u root pictrs apk add curl
# get a partial list of old post thumbnails for deletion. some thumbnails are hosted on remote instance so ignore them. remove the limit or run the script multiple times to delete all of them. you can also change the interval
docker-compose exec postgres psql -U lemmy -t -c "select thumbnail_url from post where thumbnail_url is not null and published < now() + interval '6 months' and thumbnail_url like '$MY_INSTANCE%' limit 100;" > $THUMBNAILS_LIST
# loop through the thumbnails. probably very slow to do this one by one for thousands of items
while read p; do
echo "Deleting $p"
# extract image alias (filename) from url
[[ $p =~ $REGEX ]]
ALIAS=${BASH_REMATCH[1]}
# call pictrs api to delete the file
docker-compose exec pictrs curl -H "x-api-token: $PICTRS_API_KEY" "http://localhost:8080/internal/delete?alias=$ALIAS"
# also delete thumbnail from lemmy db
docker-compose exec postgres psql -U lemmy -t -c "update post set thumbnail_url = null where thumbnail_url = '$p'"
done <$THUMBNAILS_LIST
# also consider this endpoint to cleanup image variants
#docker-compose exec pictrs curl -H "x-api-token: $PICTRS_API_KEY" http://localhost:8080/internal/variants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment