Clear out ActiveStorage

How can I delete all Active Storage records from my database but WITHOUT removing the actual files themselves (with the exception of the generated variant files).

I’ve tried several options but either get stuck removing the records or the variant files.

I saw online that variants were stored in a variants folder but when I check my S3 bucket they just show up in the root. Why is that? If I had them in a variants folder I could easily delete that.

To remove all variants and their files:

ActiveStorage::VariantWithRecord.includes(:blob).find_each do |variant|
  variant.blob.delete # This will delete the files in the services
end
ActiveStorage::VariantWithRecord.delete_all

To delete all active storage records without deleting the original files themselves:

ActiveStorage::Attachment.delete_all
ActiveStorage::Blob.delete_all

Thankyou! I will certainly be giving this a go!