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

**URL:** https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355
**Category:** Support
**Created:** [June 7, 2018, 1:03pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355 "2018-06-07T13:03:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Bryan\_Underwood\_CA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bryan_underwood_ca/32/95427_2.png) [@Bryan\_Underwood\_CA](https://meta.discourse.org/u/Bryan_Underwood_CA)
#### Post date: [June 7, 2018, 1:03pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/1 "2018-06-07T13:03:53Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [June 7, 2018, 2:50pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/2 "2018-06-07T14:50:00Z")

</div>

[I had a similar problem](https://meta.discourse.org/t/re-purposing-a-discourse-installation-for-a-yearly-event/34670/17).

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).

```ruby
Category.all.each { |c|
    c.topic_count = c.topics.length - 1 # -1 for about post
    c.save!
}

```

Alternatively, `rake import:ensure_consistency` from [here](https://meta.discourse.org/t/importers-for-large-forums/61681) might help, but it seems to have [destructive side effects](https://meta.discourse.org/t/importers-for-large-forums/61681/49).

---

<div class="post-metadata">

### Author: ![Bryan\_Underwood\_CA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bryan_underwood_ca/32/95427_2.png) [@Bryan\_Underwood\_CA](https://meta.discourse.org/u/Bryan_Underwood_CA)
#### Post date: [June 7, 2018, 3:21pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/3 "2018-06-07T15:21:38Z")

</div>

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 🚒.

[comment redacted (figured it out)]

---

<div class="post-metadata">

### Author: ![Bryan\_Underwood\_CA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bryan_underwood_ca/32/95427_2.png) [@Bryan\_Underwood\_CA](https://meta.discourse.org/u/Bryan_Underwood_CA)
#### Post date: [June 7, 2018, 5:37pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/4 "2018-06-07T17:37:19Z")

</div>

Now, I have a **new problem** which I don’t know how to feel about…

After import, I expected to have 2158 topics (or thereabout):

 ![image](https://global.discourse-cdn.com/meta/original/3X/f/f/ffc38c800afeaacf2152980fc3ea2686eb1fabca.png)

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.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [June 7, 2018, 8:16pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/5 "2018-06-07T20:16:29Z")

</div>

What does `Topic.count` returns (when executed in the rails console)?

---

<div class="post-metadata">

### Author: ![Bryan\_Underwood\_CA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bryan_underwood_ca/32/95427_2.png) [@Bryan\_Underwood\_CA](https://meta.discourse.org/u/Bryan_Underwood_CA)
#### Post date: [June 7, 2018, 8:18pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/6 "2018-06-07T20:18:19Z")

</div>

> [@Bryan\_Underwood\_CA](#):
>
> However, my new category count is around 1700

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.

---

<div class="post-metadata">

### Author: ![Bryan\_Underwood\_CA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bryan_underwood_ca/32/95427_2.png) [@Bryan\_Underwood\_CA](https://meta.discourse.org/u/Bryan_Underwood_CA)
#### Post date: [June 11, 2018, 4:04pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/8 "2018-06-11T16:04:39Z")

</div>

**FIXED.**

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

I happened to find this idea by @simon

> [@Post is deleted and shows as hidden in topic, not marked as hidden or deleted in db](https://meta.discourse.org/t/post-is-deleted-and-shows-as-hidden-in-topic-not-marked-as-hidden-or-deleted-in-db/82216/2):
>
> Through the rails console you’ll need to search with something like `Post.with_deleted.where("topic_id = ? AND deleted_at IS NOT NULL", your_topic_id)`

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):

```plaintext
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"

```

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [June 8, 2024, 12:37pm UTC](https://meta.discourse.org/t/updating-topic-count-in-categories-after-bulk-delete-missing-topics-posts/89355/9 "2024-06-08T12:37:19Z")

</div>

This topic was automatically closed after 2192 days. New replies are no longer allowed.
