Updating topic count in categories after bulk delete & missing topics/posts

FIXED.

[I deleted my other replies in this topic, since they were the wrong path]

I happened to find this idea by @simon

This helped me to see that the Post.find_each method was not returning deleted posts. So when I went to re-import, my data it still existed. Now, before I re-import I run this for topics (and separately, posts):

require_relative '../discourse/config/environment'

cnt = 0
#Topic.find_each do |t| # only returns active topics
Topic.with_deleted.find_each do |t| # returns both active and deleted topics
  if t.id > 6 && t.user_id != -2 # excludes included system topics and Discobot user Greeting messages
    puts "Topic: #{t.id}, #{t.title}, #{t.user_id}"
    t.destroy
        cnt = cnt +1
  end
end
puts "Deleted #{cnt} active and deleted topics where t.id > 6 && t.user_id != -2"
1 Like