Remove all data from Active Storage?
Asked Answered
L

3

15

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

Lenticel answered 4/7, 2018 at 14:24 Comment(8)
Mass-deleting is easy: just go to your storage provider and remove the files. But presumably you also want to do something with the references in your db? In which case you could iterate all your objects and delete their fiels individually.Promissory
@SergioTulentsev thank you for the answer. What if I want to reinstall the Active Storage? How can I do that? Drop the tables and then do rails active_storage:install ?Lenticel
What do you mean, "reinstall active storage"? To accomplish what?Promissory
You could simply empty/truncate the tables, no? But that would leave the actual files intact.Promissory
I'm getting some erros with the active storage, and I think I delete something important when I tried to delete an attachment, a while ago. Now every time I try to create a new object on a determined model, I get an error about: Can'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.Lenticel
I kinda doubt it would help (this reinstallation is not at all like, say, reinstallation of windows), but you can try. ¯\_(ツ)_/¯Promissory
Sounds like you don't have any valuable data in your db. You could even drop the database and recreate from scratch (in which case, no need for rails active_storage:install)Promissory
Yeah, I doubt it too, but I'm all in now xD But maybe I will try drop the database as you said. I will tell you later if it works. Thanks ;)Lenticel
A
42

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

  • 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.

Atrophy answered 4/7, 2018 at 18:6 Comment(6)
wooww! Thank you mate for the effort! It will help in the future for sure! I end up dropping the database and create a new one, and now everything works fine so I can't test your solution, sorry. But thank you anyway. Best regards ;)Lenticel
Glad if was helpful. Thanks.Atrophy
That helped me to achieve something else.Laywoman
To purge all Blobs O needed to run ActiveStorage::Blob.all.each { |blob| blob.purge } as wellRintoul
Doesn't this also delete files from S3 if your app is online ?Eudemonism
@Eudemonism Yes it will, I verified it for our app on PROD.Synergetic
S
2

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.

Shamus answered 2/7, 2019 at 14:29 Comment(1)
This is no good now that the default storage location for the sqlite db is storage/Remonstrant
G
2

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;
Globose answered 2/9, 2020 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.