# Force recount of topics in category after moving them

**URL:** https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026
**Category:** Support
**Created:** [30 oktober 2017 om 03:37 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026 "2017-10-30T03:37:10Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![omarfilip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/omarfilip/32/208019_2.png) [@omarfilip](https://meta.discourse.org/u/omarfilip)
#### Post date: [30 oktober 2017 om 03:37 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/1 "2017-10-30T03:37:10Z")

</div>

Can the topic count for a category be forced to update after the topics have been moved to a different category?

I used this method to move topics around after merging two forums, but the counts are still off after 1.5 days.

[https://meta.discourse.org/t/bulk-move-many-topics-from-one-category-to-another/38704/2](https://meta.discourse.org/t/bulk-move-many-topics-from-one-category-to-another/38704/2)

> [@Bulk move many topics from one category to another](https://meta.discourse.org/t/bulk-move-many-topics-from-one-category-to-another/38704/2):
>
> If you execute either of these, category stats will be wrong but should fix themselves after a day.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [30 oktober 2017 om 04:28 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/2 "2017-10-30T04:28:23Z")

</div>

Shouldn’t the category counts rebuild after a day @neil?

---

<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: [30 oktober 2017 om 09:20 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/3 "2017-10-30T09:20:25Z")

</div>

It would be nice if they did! However, that didn’t work out for us [when we did a similar move](https://meta.discourse.org/t/re-purposing-a-discourse-installation-for-a-yearly-event/34670/17).

The following Ruby code, executed in the Rails console, will either fix the issue or set your database on fire 🔥, so backup accordingly 😉

```ruby
Category.find_each { |c|
    c.topic_count = Topic.where(category: c).count - 1 # -1 for about post
    c.save!
}

```

(I’d still love for these to be re-counted occasionally, [just as I’d do for post counts](https://meta.discourse.org/t/wrong-post-count-in-profile/71352/41) 🙂)

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [30 oktober 2017 om 17:39 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/4 "2017-10-30T17:39:50Z")

</div>

Yes this is happenings [once a day](https://github.com/discourse/discourse/blob/master/app/jobs/scheduled/category_stats.rb), doing this to update the counts:

> <https://github.com/discourse/discourse/blob/main/app/models/category.rb#L137-L151>

Seems to work for me.

@fefrei @omarfilip Please go to sidekiq scheduler (`/sidekiq/scheduler`) and see what the last run of CategoryStats shows.

 ![27 PM](https://global.discourse-cdn.com/meta/original/3X/5/8/58d0caf9fb2fbc0482b6165e1ed83544f18002aa.jpg)

---

<div class="post-metadata">

### Author: ![omarfilip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/omarfilip/32/208019_2.png) [@omarfilip](https://meta.discourse.org/u/omarfilip)
#### Post date: [30 oktober 2017 om 17:52 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/5 "2017-10-30T17:52:34Z")

</div>

I’ve triggered mine manually yesterday so the scheduler won’t mean much right now:

 ![28 AM](https://global.discourse-cdn.com/meta/original/3X/a/7/a76377b1f029a1200b25ee020e08dd728bef3fe4.png)

The manual triggering of CategoryStats did not change the counts, though.

I ran Felix’s ruby code earlier today and that dropped the topic counts in the empty categories. It did not increase the topic count elsewhere like I thought it would.

---

<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: [30 oktober 2017 om 18:02 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/6 "2017-10-30T18:02:22Z")

</div>

I had also triggered that job manually, as far as I recall. It completed successfully, but had no effect.

---

<div class="post-metadata">

### Author: ![jerdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jerdog/32/122843_2.png) [@jerdog](https://meta.discourse.org/u/jerdog)
#### Post date: [23 januari 2018 om 14:27 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/7 "2018-01-23T14:27:59Z")

</div>

Mine also ran but didn’t change the counts. I also have one which is `-1` as well.

![image](https://global.discourse-cdn.com/meta/optimized/3X/b/8/b807e60ec22d327295dbd9520f4f85ee0603d111_2_690x39.png)

I will run the code from @fefrei and see if that makes a difference.

Edit: running the code worked in the rails console

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [14 april 2020 om 15:04 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/8 "2020-04-14T15:04:41Z")

</div>



---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [13 mei 2024 om 17:03 UTC](https://meta.discourse.org/t/force-recount-of-topics-in-category-after-moving-them/73026/9 "2024-05-13T17:03:04Z")

</div>


