I would like to know how can I delete all data from Active Storage or even resetting Active Storage? There is any way to do that? Thank you in advance!
NOTE: I'm using Rails 5.2
I would like to know how can I delete all data from Active Storage or even resetting Active Storage? There is any way to do that? Thank you in advance!
NOTE: I'm using Rails 5.2
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:
To completely clean the two tables, I did in rails console
:
ActiveStorage::Attachment.all.each { |attachment| attachment.purge }
This command deletes
ActiveStorage::Attachment.any? #=> false
ActiveStorage::Blob.any? #=> false
/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.
ActiveStorage::Blob.all.each { |blob| blob.purge }
as well –
Rintoul On my local development environment, whenever I perform rails db:reset
, I also run rm -rf storage
to clear all previously-saved files. Rails will automatically recreate the storage
directory the next time a file is uploaded.
No doubt ActiveStorage::Attachment.all.each { |attachment| attachment.purge }
will purge all records, but it will take long time if you have lots of files.
For development env, you can simply remove all the attachment records from the database and remove
files from the storage
folder.
run rails dbconsole
and execute the following queries to delete the attachment records.
delete from active_storage_attachments;
if you are storing variant in your database:
delete from active_storage_variant_records;
then finally,
delete from active_storage_blobs;
© 2022 - 2024 — McMap. All rights reserved.
rails active_storage:install
? – LenticelCan't resolve image into URL: undefined method "signed_id" for nil:NilClass
. So, I think If I reset/drop the active storage tables tables, might work, I don't know. That's why I want to delete the tables and create new ones. – Lenticelrails active_storage:install
) – Promissory