January 18, 2017

Terminal Commands To Clean Up Excess WordPress Image Sizes

This past month when cleaning out my website in an attempt to move hosts I discovered I had over 70,000 images in my WordPress uploads directory. Even after 6 years of blogging that still seemed like a lot to me…

It turns out that many of these images were thumbnails of various sizes generated by themes I had cycled through over the years. These themes, whenever I would regenerate thumbnails to accomodate them, would bloat up my directory.

Finally after failing to use one of the many plugins out there, like thumbnail cleaner, to rid my uploads folders I had to get creative.

The Terminal

Below are a couple of terminal commands I used to search and destroy unused images.

Search By Size

First I searched for the largest files by navigating to the uploads director and entering the below command.

find . -size +10M -ls

This command finds and lists the largest files in a directory. Anything above 10MB. The files I found only accounted for a small portion of the files in my uploads folder.

By File Pattern

Something I noticed while browsing by file size was that all files ended with the relatively same pattern of something like -150×150.jpg.

From there I decided to remove all of the file sizes generated by WordPress that I was fairly confident I hadn’t used on my site, or didn’t use anymore at least. All of the WordPress generated files used the same pattern at the end so I came up with the below commands.

find . -name *-450x450.* | xargs rm -f
find . -name *-650x650.* | xargs rm -f
find . -name *-600x300.* | xargs rm -f
find . -name *-960x300.* | xargs rm -f
find . -name *-180x137.* | xargs rm -f
find . -name *-150x75.* | xargs rm -f
find . -name *-50x50.* | xargs rm -f
find . -name *-66x66.* | xargs rm -f
find . -name *-52x50.* | xargs rm -f
find . -name *-120x120.* | xargs rm -f
find . -name *-100x100.* | xargs rm -f
find . -name *-500x500.* | xargs rm -f
find . -name *-800x800.* | xargs rm -f
find . -name *-500* | xargs rm -f
find . -name *-700* | xargs rm -f
find . -name *-300x225.* | xargs rm -f

 In reality you could probably do something like the below to just wipe out all WordPress generated thumbnails.

find . -name *-*x*.* | xargs rm -f


I dropped my uploads directory for 70,000 images to 7,000 and also managed to drop the size of my WordPress website from 12GB to something more like 3GB. It made all the difference when trying to migrate a website!

Related Marketers

Related Tools

Related Books

Related Posts

Notify of

Inline Feedbacks
View all comments