# Accurate topic count in tag list

**URL:** https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989
**Category:** Feature
**Created:** [March 28, 2020, 10:35pm UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989 "2020-03-28T22:35:11Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![syl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/syl/32/98883_2.png) [@syl](https://meta.discourse.org/u/syl)
#### Post date: [March 28, 2020, 10:35pm UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/1 "2020-03-28T22:35:11Z")

</div>

In the `tags` page, the topic number is not accurate. Indeed, it includes topics that are not visible to the user.

Example here on meta:

- Page “[Discourse Meta](https://meta.discourse.org/tags)” says there are 4 topics in the `bug` tag:

- But page “[Topics tagged bug](https://meta.discourse.org/tag/bug)” is empty:

I’ve been hit by this issue when using the API. In my plugin, I need an exact topic count, so I’m forced to load each tag in order to count the visible topics. It seems to work right now, but it’s a waste of bandwidth and I’m afraid one day I will hit a Discourse rate limit for API calls.

Any chance this might be solved?

Thanks.

---

<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: [March 29, 2020, 2:54am UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/2 "2020-03-29T02:54:05Z")

</div>

Those are all in PMs you don’t have access to.

It’s not a fix or anything (obviously) but at 4 instances only in PMs I don’t think we need this particular tag .. so I’m removing it.

---

<div class="post-metadata">

### Author: ![syl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/syl/32/98883_2.png) [@syl](https://meta.discourse.org/u/syl)
#### Post date: [March 29, 2020, 8:26am UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/3 "2020-03-29T08:26:02Z")

</div>

If need be, another example on meta is the `sass` tag (topic count in tag list=2, topic count in tag page=1).

---

<div class="post-metadata">

### Author: ![ninjapenguin](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@ninjapenguin](https://meta.discourse.org/u/ninjapenguin)
#### Post date: [July 2, 2020, 2:27pm UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/4 "2020-07-02T14:27:24Z")

</div>

I’ve noticed this in our forum also. The easiest way to recreate it is if i create a new topic and tag then delete it, it is still counted.

---

<div class="post-metadata">

### Author: ![syl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/syl/32/98883_2.png) [@syl](https://meta.discourse.org/u/syl)
#### Post date: [August 24, 2020, 12:23pm UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/5 "2020-08-24T12:23:12Z")

</div>

An additional minor annoyance caused by this issue is that admins get a wrong error message when deleting an empty tag.

Empty tag:

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

Wrong error message:

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

---

<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: [August 25, 2020, 12:12am UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/6 "2020-08-25T00:12:25Z")

</div>

Can you repro this @tshenry?

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [August 25, 2020, 12:48am UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/8 "2020-08-25T00:48:35Z")

</div>

Ok, I’m re-doing my response as I’m pretty sure I figured out everything that’s going on here 🙂

As @ninjapenguin mentions, if you have a tag and delete all of the topics that have that tag applied, the tag remains listed on the tags page and the count unchanged. I believe this is happening because the topics still _technically_ exist and they continue to hold the tag.

@syl if you navigate to the `tete` tag’s topic list as shown in your screenshot and then add

```plaintext
?status=deleted

```

to the end of the URL, you should see two deleted topics listed with the `tete` tag.

The information message in your second screenshot corresponds to the the number of deleted topics that have the tag applied.

* * *

I feel like we probably shouldn’t factor in deleted topics when counting/listing tags, but this may not be a super trivial change given how everything is stored in the database. What do you think @codinghorror?

---

<div class="post-metadata">

### Author: ![syl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/syl/32/98883_2.png) [@syl](https://meta.discourse.org/u/syl)
#### Post date: [August 25, 2020, 8:47am UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/9 "2020-08-25T08:47:26Z")

</div>

> [@tshenry](#):
>
> @syl if you navigate to the `tete` tag’s topic list as shown in your screenshot and then add `?status=deleted` to the end of the URL, you should see two deleted topics listed with the `tete` tag.

That is correct.

In a standard forum, I think deleted topics explain most discrepancies between the topic count displayed in the tag list and the actual number of topics a user can see within each tag. Fixing this would already be a big plus. But notice that discrepancies also occur when a non-admin user is looking at a tag containing private topics.

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [August 25, 2020, 6:22pm UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/10 "2020-08-25T18:22:24Z")

</div>

> [@syl](#):
>
> discrepancies also occur when a non-admin user is looking at a tag containing private topics.

This I cannot reproduce. If I have a tag that has **only** been used on a PM or a topic in a restricted category, a user without access to those topics does not see the tag included in the tag list.

The only unfortunate behavior I’m seeing at the moment is what I mentioned surrounding deleted topics.

---

<div class="post-metadata">

### Author: ![syl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/syl/32/98883_2.png) [@syl](https://meta.discourse.org/u/syl)
#### Post date: [August 26, 2020, 8:08am UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/11 "2020-08-26T08:08:57Z")

</div>

> [@tshenry](#):
>
> This I cannot reproduce. If I have a tag that has **only** been used on a PM or a topic in a restricted category, a user without access to those topics does not see the tag included in the tag list.

Sorry for the confusion, I was talking about the tag count. Here is how to reproduce the issue:

1. As an admin, create a topic with no category and with a new tag  
 ![image](https://global.discourse-cdn.com/meta/original/3X/3/d/3d9366980463ac9ec045a324f8f298459fe597e1.png)
2. As an admin, create another topic with the same tag and belonging to a restricted category  
 ![image](https://global.discourse-cdn.com/meta/original/3X/1/0/104df80f468308ed45e4ea4aaa1842bfb6dec8b3.png)
3. As a normal user, go to the `test-tag` tag and check that you can see only one topic  
 ![image](https://global.discourse-cdn.com/meta/original/3X/b/1/b16d31ebd85ea35b72be2ee566991ca7df32963e.png)
4. But in the tag list, the tag count shows 2  
 ![image](https://global.discourse-cdn.com/meta/original/3X/d/3/d38acebfdc7614aeef8872c668e4576f7bcb8537.png)

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [August 26, 2020, 5:55pm UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/12 "2020-08-26T17:55:28Z")

</div>

Ok thanks, I see that now.

I took a look at the code and have noticed a few things.

- There are two columns in the `tags` table that can influence the tag count: `topic_count` and `pm_topic_count`. These counts are [incremented/decremented on tag application/removal](https://github.com/discourse/discourse/blob/master/app/models/topic_tag.rb). Non-admin users only ever see the value of `topic_count`.

- There is a [consistency job](https://github.com/discourse/discourse/blob/03818e642a1ae871bffdc0c39c10f05f0b8b0398/app/jobs/scheduled/ensure_db_consistency.rb) that runs every 12 hours that will [update the tag counts](https://github.com/discourse/discourse/blob/298ed5d0211a0f98ec452b06385b13d36f1d643c/app/models/tag.rb#L55-L91) so that they **do not** include deleted topics.

- There is nothing to adjust the count on the fly based on a user’s individual topic access.

Based on what I’m seeing, we don’t have a bug, just a limitation based on the current implementation. I think everything that has been talked about here falls under the “feature request” banner and can be summarized as:

> Make tag counts on the tags page dynamic so that they reflect what a user will actually see when they navigate to the tag’s topic list.

---

<div class="post-metadata">

### Author: ![syl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/syl/32/98883_2.png) [@syl](https://meta.discourse.org/u/syl)
#### Post date: [August 26, 2020, 9:38pm UTC](https://meta.discourse.org/t/accurate-topic-count-in-tag-list/145989/13 "2020-08-26T21:38:21Z")

</div>

> [@tshenry](#):
>
> There is a [consistency job](https://github.com/discourse/discourse/blob/03818e642a1ae871bffdc0c39c10f05f0b8b0398/app/jobs/scheduled/ensure_db_consistency.rb) that runs every 12 hours that will [update the tag counts](https://github.com/discourse/discourse/blob/298ed5d0211a0f98ec452b06385b13d36f1d643c/app/models/tag.rb#L55-L91) so that they **do not** include deleted topics.

Thanks a lot @tshenry. That sounds very good, and might explain why a got the issue a lot more on my dev instance (maybe it doesn’t run the consistency job). I will check this.

P.S.: right now, the `documentation` tag here on Meta has a count of 5 but only 4 topics that I can see. I will check again in 12 hours 🙂
