Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all data from Active Storage?

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

like image 756
Proz1g Avatar asked Jul 04 '18 14:07

Proz1g


People also ask

How do I delete active storage attachments?

To remove an attachment from a model, call purge on the attachment. If your application is set up to use Active Job, removal can be done in the background instead by calling purge_later . Purging deletes the blob and the file from the storage service.

What does active storage mean?

1 What is Active Storage? Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects.

What is Active storage in rails?

Active storage is an inbuilt gem in Rails that developers widely use to handle file uploads. Combined with the encrypted credentials feature in the latest releases of Rails, active storage is a safe and easy method to upload, serve, and analyze files onto cloud-based storage services as well as local storage.


1 Answers

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.

like image 109
iGian Avatar answered Sep 25 '22 14:09

iGian