Help deleting photos from server that were attached to deleted posts

I don’t think the post_uploads table exists any more. It’s now uploads and upload_references. You may need to update that old snippet to take that into account if you’re trying that method.

How have you deleted those posts? Just soft-deleting in the UI? I think the setting that cleans up orphaned images (clean orphan uploads grace period hours) doesn’t take into account whether they’re soft-deleted, and only cares if the image is still in the latest version of the post:

I think you can select all the posts in the rails console and use the PostDestroyer on them to hard delete them, and then the uploads will get cleaned up the next time Jobs::CleanUpUploads runs (or is manually triggered).

If it’s a whole category, for instance, I think you can use something like this:

category = Category.where(id: CATEGORY_ID).pluck(:id)  
topic = Topic.where(category_id: category).pluck(:id)

topic.each do |t|
  Post.where(topic_id: t).find_each do |p|
    PostDestroyer.new(Discourse.system_user, p).destroy
    p.destroy! 
  end
end

I think there’s also some conversation in this topic too that you may find useful Delete deleted-posts permanently in bulk? - #57 by Simon_Manning

:warning: And I’ll add the usual warning about taking a backup before making changes in the rails console, just in case. Sharp Knife alert. :rotating_light:

(Just an FYI, but Jobs::DirectoryRefreshDaily and Jobs::DirectoryRefreshOlder are the ones that populate the User Directory, so aren’t relevant here)

5 Likes