Remove all data from Active Storage?
This question challenged me, so I did some test on my dummy app with local storage.
I have the usual model User
which has_one_attached :avatar
On local storage files are saved on /storage
folder, under subfolders named randomly with a string of two characters.
Informations related to files are stored in two tables:
- ActiveStorage::Attachment
- ActiveStorage::Blob
To completely clean the two tables, I did in rails console
:
ActiveStorage::Attachment.all.each { |attachment| attachment.purge }
This command deletes
- All record in that table:
ActiveStorage::Attachment.any? #=> false
- All the blobs:
ActiveStorage::Blob.any? #=> false
- All the files located under
/storage
subfolders; of course, subfolders are still there empty.
The ActiveStorage still works poperly.
I expect the same behaviour for remote storage, having the right privileges.
Rails 5.2 Active Storage purging/deleting attachments
You are looping through the collection of images and calling the purge method on each one. Instead you should be linking to a destroy method on your controller, something like the below taking into account your controller actions and guessing at your route names.
The error is because image object returns its full path and the link thinks that what you want to point to. Instead you just want its signed_id
and want the link to call the route that has your delete_image_attachment
path.
<%= link_to 'Remove', delete_image_attachment_collections_url(image.signed_id),
method: :delete,
data: { confirm: 'Are you sure?' } %>
The destroy method would look something like this...
def delete_image_attachment
@image = ActiveStorage::Blob.find_signed(params[:id])
@image.purge
redirect_to collections_url
end
The route should be something like so...
resources :collections do
member do
delete :delete_image_attachment
end
end
Check out the rails routing guide for more fun routing facts.
Rails 6 active storage delete attachment doesn't delete file from server
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.
Rails active storage issue with deleting individual uploads
Everything was set up correctly from the get go. The problem was that I had accidentally removed the include for application.js from my project. The lack of UJS reference was causing all requests to be gets instead of the delete in my case.
Rails Active Storage, delete file from S3 bucket
I don't know what was the problem but restarting my rails console fix the problem.
Related Topics
How to Install Libksba on MAC Osx
Ruby, How to Access Local Variables Outside the Do - End Loop
Why Are My Rspec Tests Failing, But My App Is Working
Could Not Find Gem 'Logstash-Devutils (>= 0) Ruby' in Any of the Gem Sources
Using $1, $2, etc. Global Variables Inside Method Definition
Require Command Not Working Within Bash Irb on Snow Leopard
How to Install Rmagick Gem on Windows
Ruby Equivalent of C#'s 'Yield' Keyword, Or, Creating Sequences Without Preallocating Memory
What Are the Meanings of the Hash Keys When Calling Objectspace.Count_Objects
Error When Installing Libv8 3.11.8.3
What Are Fast Xml Parsers for Ruby
Why Does 6.Times.Map Work in Ruby 1.8.7 But Not 1.8.6
Action Mailer Smtp Google Apps
What Does a Single Splat/Asterisk in a Ruby Argument List Mean
Ruby Spreadsheet Row Background Color