Delete deleted-posts permanently in bulk?

Excellent, glad to hear that did the job for you. So for the sake of combining the answers, one can use the following in the Rails console to destroy all topics that were deleted more than 90 days ago, repeating as many times as necessary if there are more than 1000 topics:

Topic.with_deleted.where(deleted_at: ...90.days.ago).limit(1000).destroy_all

After that has completed, the following can be used to destroy all posts that have been orphaned from destroyed topics:

Post.find_by_sql("select * from posts where topic_id not in (select id from topics)").each { |p| p.destroy }

It’s worth noting that the above commands will not destroy deleted posts however, only deleted topics and their orphaned posts. To also destroy deleted posts older than 90 days, use the following, again repeating as necessary:

Post.with_deleted.where(deleted_at: ...90.days.ago).limit(1000).destroy_all

P.S. Out of interest, did you try destroy_all without limit(1000) and have problems or did you not try it without the limit?

8 Likes