12

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

Proz1g
  • 1,177
  • 4
  • 16
  • 36
  • 2
    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](http://edgeguides.rubyonrails.org/active_storage_overview.html#removing-files). – Sergio Tulentsev Jul 04 '18 at 14:26
  • @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` ? – Proz1g Jul 04 '18 at 14:50
  • What do you mean, "reinstall active storage"? To accomplish what? – Sergio Tulentsev Jul 04 '18 at 14:53
  • You could simply empty/truncate the tables, no? But that would leave the actual files intact. – Sergio Tulentsev Jul 04 '18 at 14:55
  • 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. – Proz1g Jul 04 '18 at 14:57
  • I kinda doubt it would help (this reinstallation is not at all like, say, reinstallation of windows), but you can try. ¯\\_(ツ)_/¯ – Sergio Tulentsev Jul 04 '18 at 15:00
  • 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`) – Sergio Tulentsev Jul 04 '18 at 15:01
  • 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 ;) – Proz1g Jul 04 '18 at 15:05

3 Answers3

40

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.

iGian
  • 11,023
  • 3
  • 21
  • 36
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;
Kishan Ku. Patel
  • 311
  • 1
  • 2
  • 10
1

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.

jabbett
  • 582
  • 1
  • 7
  • 20