Search code examples
ruby-on-railsrails-activestorage

Purge blobs by default in Rails 6


Removing an ActiveStorage object in Rails 6 does not purge the blob. It deletes the record form the database but the file still exists. One needs to manually call purge or purge_later in order to permanently remove the file.

This took me by surprise, why would you want to remove the reference but keep the file?

Is there a way to change the default and tell Rails to always purge the blob/file if the record is removed?

Alternatively I could manually check if any attachments have been removed in an after_save hook or have a cronjob to remove all orphaned files periodically. What would be the most idiomatic way to approach this?


Solution

  • Found it. I am using the Apartment gem and forgot to exclude the ActiveStorage models. The Apartment gem uses database views so it would remove the reference from the database but I guess the purge_later does not run in the same database scope.

    Excluding the ActiveStorage models in the config/initializers/apartment.rb did the trick.

    config.excluded_models = %w{ ActiveStorage::Attachment ActiveStorage::Blob }