Search code examples
ruby-on-railsgoogle-cloud-storageruby-on-rails-6rails-activestorage

Rails 6 active storage delete attachment doesn't delete file from server


When using active storage, one can delete images like so:

@file = ActiveStorage::Attachment.find(params[:id])
@file.purge

However, this only deletes the database records.

How do you clean up files on the server when deleting attachments using ActiveStorage?


Solution

  • The documentation for purge says that it does delete the file off of the server:

    Deletes the file on the service and then destroys the blob record. This is the recommended way to dispose of unwanted blobs. Note, though, that deleting the file off the service will initiate a HTTP connection to the service, which may be slow or prevented, so you should not use this method inside a transaction or in callbacks. Use purge_later instead.