Rails – Carrierwave: Remove avatar and multiple files from S3

When a User destroys a record, what’s the point of keeping the uploaded files/images with Carrierwave on S3?

It’s wasted storage space. You need to be able to remove images and files from your Model.

In this tutorial, we will see how to use the Carrierwave method :remove_avatar to delete a file uploaded on S3 from your Rails app. We will also fix the problem of leftover empty directories.

1. Delete avatar and multiple Carrierwave files from S3 on Model destroy

Carrierwave provides a convenient method for removing uploaded files if a checkbox is checked. We will use and adapt it to delete file from S3 after the Model has been destroyed.

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader

  before_destroy :clean_s3

  def clean_s3
    avatar.thumb.remove! # if you have thumb version or any other version
  rescue Excon::Errors::Error => error
    puts "Something gone wrong"

This code comes from this nice StackOverflow answer.

To be complete, let’s say your model stores several files like Word documents and PDF, and not images avatar like the example on the docs, and you have the line mount_uploaders :attachments, AttachmentUploader in your model.

You’ll want to iterate through each attachment to remove it one at a time. Here is how:

def clean_s3
rescue Excon::Errors::Error => error
  puts "Couldn't be removed"


2. Delete empty directories

After implementing step 1 of this Carrierwave tutorial and destroying the object, you’ll notice that empty directories remain in the tmp cache directory.

While unpleasant, it’s not really a problem unless you have a very large amount of them.

But to keep things tidy, let’s see how to remove them. We will implement this a simple version of this solution from the wiki.

Your AttachmentUploader will look like this:

class AttachmentUploader < CarrierWave::Uploader::Base
  after :remove, :delete_empty_upstream_dirs # Clean up empty directories when the files in them are deleted

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir

  def delete_empty_upstream_dirs
    path = ::File.expand_path(store_dir, root)
    Dir.delete(path) # fails if path not empty dir, beware ".DS_Store" when in development  
  rescue SystemCallError
    true # nothing, the dir is not empty


SEO for Ruby on Rails – The Complete Guide (2017)

There are many ways to improve the SEO for Ruby on Rails.

And SEO is one of the best free ways to gain new customers for a start-up.

So here is a guide with many tips to improve Rails SEO.

There are numerous quick wins to be made, and spending a few minutes on those can dramatically improve your rankings in Google and other search engines. We tried to bring everything that can be useful for a Rails app.

Read more

How to add a WordPress blog to a Ruby on Rails app?

While Ruby on Rails is what’s best to build a web app, WordPress is number #1 when it comes to blogging. And these days, a great start-up must have a blog alongside its main app. So, let’s see how to add a WordPress blog to Ruby on Rails. Read more

How to Add jQuery Validation in your Rails 5 Forms Without a Gem

Some kind of jQuery validation should be implemented on any Rails form. It’s not always straightforward, but your users need it. There is nothing worse than to fill in many fields, submit the form, waiting for the page to load and then be told that what you did was not right.
Think of your own interactions. How better is it when the form tells you what’s wrong right away?

Read more

Rails 5: How to remove Turbolinks 5

Hello World. Want to disable Turbolinks? While it does great things, not all Rails apps need it. Do you have weird bugs with bugs with Javascript? It might be because of it. So, here is how to remove Turbolinks.  Read more