How to Delete Unused images from Rails Application

How to Delete Unused images from Rails Application

27 Sep 2022
0 Comments
How to Delete Unused images from Rails Application

In a Rails application, unnecessary images will affect application load time and also slow down your test and deploy pipeline.

One of my recent projects was to try to identify unused images and remove them safely. It was quite a difficult task in the application to find images that were not in use.

Really It’s hard to check if you’re not using each one of them, so you can use the below Rake task to flush out all the unused images and delete them.


So here is a Rake task to find unused images in the Rails project for deletion.
 

1) How to Create Rake task?

Command: rails g task <namespace> <taskname>

For Eg: rails g task image_cleaner find_unused_images

rake file will generate inside the “lib/tasks” folder.

Rake file body:


How to execute?

Run below rake task command in your project directory terminal:


Execution result:

As for me, I have successfully deleted unused images by using the above rake task without breaking anything. hopefully, now you would love to delete all the images in your app/assets/images folder that you don't use.

Hope this helps!

Thanks for reading.




Leave a comment: