Rails 6 活动存储删除附件不从服务器删除文件

Rails 6 Active Storage Delete Attachment Doesn't Delete File From Server

使用动态存储时,可以像这样删除图片:

@file = ActiveStorage::Attachment.find(params[:id])
@file.purge

但是,这只会删除数据库记录。

问题 - 使用 ActiveStorage 删除附件时清理服务器上文件的最佳做法是什么?

答案应适应不同的存储服务,例如本地、S3、Google 云存储等

purge 的文档说它确实从服务器上删除了文件:

Deletes the file on the service and then destroys the blob record. This is the recommended way to dispose of unwanted blobs. Note, though, that deleting the file off the service will initiate a HTTP connection to the service, which may be slow or prevented, so you should not use this method inside a transaction or in callbacks. Use purge_later instead.