How to Delete Unused images from Rails Application

How to Delete Unused images from Rails Application

29 Sep 2021
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 the unused images and remove them safely. It was quite a difficult task in the rails 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 to 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:

Rake file
 

How to execute?

Run below rake task command in your project directory terminal:

How to execute

 

Execution result:

delete images in rails application
 

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: