Delete Unused Images from a Jekyll Website using Powershell
Home
About
Articles
When I moved my website from WordPress to Jekyll, a lot of unnecessary images were transferred over that were increasing the size of the GitHub repository by as much as 40%.
As a Windows user, what better time to use PowerShell to automatically delete all of the unused images? If we’ve got a script to do the heavy lifting for us, we’ll be able to execute it any time we need to do a spring clean.
There’s a few different ways to accomplish this, but these are the steps I’ll be using:
Find the used images on the Jekyll website
Find all of the images stored in the local /assets/ folder
Compare the used images against all of the images to find the unused images
Prompt the user before deleting the unused images
To find all of the used images I’ll loop through the sitemap of the website (/sitemap.xml) and use PowerShell to detect all of the images used on each page, adding each of them to an array:
To find all of the images for the website is slightly easier. All we need to do is add all of the images in the /assets/ folder into a new array. Don’t forget to add a filter so we can ignore any CSS or other documents.
Finally we do a compare on the two arrays and prompt the user if they’d like to go ahead and delete all of the unused images:
As you can see, using PowerShell is fairly straight forward to automate this process. I’m sure I could optimize it further, but for now it works as expected and I’m able to remove all of the unused images.
Nearly a year ago I migrated from the popular blogging platform WordPress to the static site generator Jekyll. Why am I and thousands of other bloggers deciding to move to a static site generator? It’s a no-brainer! Why should we install security updates, manage plugins and tune the performance o... Read more
RSS