In a Discourse support thread titled “Updating topic count in categories after bulk delete & missing topics/posts,” Bryan_Underwood_CA sought help after bulk-deleting topics, which left category topic counts inaccurate. fefrei suggested manually updating counts via Rails console or running rake import:ensure_consistency, warning of potential risks. Bryan later discovered the solution was triggering Jobs::CategoryStats via Sidekiq, resolving the count discrepancy without incident 3. A new issue arose: after re-importing, the topic count (1691) fell short of the expected ~2158. Bryan confirmed via Rails console that Topic.count returned 1691, clarifying he meant topic count beside categories, not category count 6. The root cause was found: Post.find_each and Topic.find_each ignore deleted records. Following a tip from simon, Bryan used Topic.with_deleted.find_each to locate and destroy lingering deleted topics before re-importing, successfully fixing the count mismatch 8. The thread highlights the importance of handling soft-deleted records and using Sidekiq jobs for accurate category stats in Discourse.
Thanks @pfaffman for the bulk deletion snippet you wrote using the Rails find_each method. Great work.
After bulk back-end deletion, is there a way to update the topic count in categories? Right now, I have an empty topic list which is ready for fresh import but the topic count in my categories still shows a count of thousands.And when I re-import fresh, the number only grows.
I waited overnight since I am not sure if there is a cron job or background task in Discourse that updates the counts. (I’m new to Discourse)
I feel like there is a command to refresh_topic_count or something…
The following code (to be run within rails console) may solve your problem, make it worse, or set your hair on fire . Use at your own risk (and with a backup).
Category.all.each { |c|
c.topic_count = c.topics.length - 1 # -1 for about post
c.save!
}
Alternatively, rake import:ensure_consistency from here might help, but it seems to have destructive side effects.
Awesome, thanks @fefrei . Since I am new to Discourse and apparently did not completely read the docs I was not even aware that Sidekiq existed! Totally awesome. The solution was to simply trigger the Jobs::CategoryStats. No fire needed .
However, my new category count is around 1700. I checked the rails console to get (find_each) topics count where user not discobot and it confirms the new category topic count around 1700. Is there something I’m missing? Is there a log of “skipped topics” during import? I can’t seem to trace it out in the base.rb import include. Where does base.rb “puts” the error messages, exactly? Because it’s not in the ssh console.
Sorry, I meant to say my topic count not category count. Trying to say my “topic count” showing next to my “category” label. The topic count returns 1691.
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"