Skip to content

Instantly share code, notes, and snippets.

@sskylar
Last active September 11, 2024 18:51
Show Gist options
  • Save sskylar/1008a0ab7d7aed5b75bd6f6f402b0ea6 to your computer and use it in GitHub Desktop.
Save sskylar/1008a0ab7d7aed5b75bd6f6f402b0ea6 to your computer and use it in GitHub Desktop.
Find unused assets in Jekyll
#!/bin/bash
for f in _uploads/*
do
basename=$(basename "$f")
escaped_name=$(echo -n "$basename" | ruby -e "require 'jekyll'; print Jekyll::URL.escape_path ARGF.read")
found=$(ag -lQ "$escaped_name" _site || ag -lQ "$basename" _site)
if [[ -z $found ]]
then
if [[ $1 == "--delete" ]]
then
echo "Deleting... $basename"
rm "$f"
else
echo "$basename"
fi
fi
done
@sskylar
Copy link
Author

sskylar commented Sep 11, 2024

Before running, build site (including drafts):

bundle exec jekyll build --unpublished --future --drafts

Dry run:

./unused-uploads.sh

Delete unused uploads:

./unused-uploads.sh --delete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment