Help deleting photos from server that were attached to deleted posts

AI-generated summary

A user (flash) is struggling to delete orphaned uploads after removing thousands of posts — uploads size increased instead of decreasing. They tried SQL commands from older threads and manually triggered Sidekiq jobs like Jobs::CleanUpUploads and Jobs::PurgeDeletedUploads, but nothing worked. Another user (JammyDodger) explains that the old post_uploads table no longer exists — uploads are now tracked via uploads and upload_references. Soft-deleted posts don’t trigger upload cleanup; only hard-deleted posts do. JammyDodger suggests using PostDestroyer in the Rails console to hard-delete posts (or entire categories) and then manually run Jobs::CleanUpUploads. They also caution to backup before running console commands. The user (flash) confirms they’ve only soft-deleted posts and categories on a test site with no users, and orphaned uploads still haven’t been purged despite settings like ‘orphan uploads delete in an hour’. JammyDodger notes that DirectoryRefresh jobs are unrelated to upload cleanup. Helpful links include meta.discourse.org/t/how-to-make-an-image-orphaned-so-that-it-gets-removed/79231 and meta.discourse.org/t/delete-deleted-posts-permanently-in-bulk/203289.

After deleting several thousand posts with photos the uploads in the admin section remains the same. This setting was set to 1. Is there a way to purge all the uploads associated with deleted posts faster?

purge deleted uploads grace period days

Grace period (in days) before a deleted upload is erased.

I did this what I saw in another post but after rebuilding nothing was gone

went into sidkiq

* Jobs::CleanUpUploads
* Jobs::PurgeDeletedUploads
* Jobs::DirectoryRefreshDaily
* Jobs::DirectoryRefreshOlder

I went from 13.7gb uploads to 14gb after deleting everything. Seems the more I delete the higher the uploads data goes.

Im not sure I understand this. I have not uploaded anything but deleted posts and categories on a test site. Theres no users right now and its been about 3 hours or so. from 13.7 to now showing this. Orphans uploads set to delete in an hour and hasnt happened.

I see this thread that discusses alot but nothing happened?

Then this one here.

and Ive tried everything in rails mentioned, sidekiq, and with commands in that thread and still nothing deletes. Over should be hundreds if not thousands of images deleted as the users posts removed posted photos with every thread.

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)