# 能够删除少于 N 个主题的标签

**URL:** https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979
**Category:** Feature
**Tags:** tags
**Created:** [2020 年8 月 6 日 05:37 UTC](https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979 "2020-08-06T05:37:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [2020 年8 月 6 日 05:37 UTC](https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979/1 "2020-08-06T05:37:40Z")

</div>

我刚刚偶然发现了这个很棒的功能“删除未使用的标签”，这让我意犹未尽！

继续讨论 [Discourse 标签的综合指南](https://meta.discourse.org/t/a-comprehensive-guide-to-discourse-tags/121041)：

> [@tshenry](#):
>
> ### 标签管理选项（管理标签组、上传标签和删除未使用的标签）
> 
> 进入您网站的标签页面，查找页面右上角的“标签管理”按钮：
> 
> ![tag3](https://global.discourse-cdn.com/meta/original/3X/c/e/ce8de9d55ecb3a4b37108dc720301052fd8a7ba4.png)

我们有许多标签的“使用次数：1”，因为标签创建完全没有受到限制，有些人只是把它们当作井号标签使用。我仍然认为应该保持标签权限的开放性，但如果能更轻松地定期清理这些“花园”就好了。

我很希望能找到方法引导人们将标签视为“归类”相关主题的手段，并仅在认为（或知道！）标签会被多次使用时才创建它们。

但在此期间，我认为扩展现有功能，允许“删除被少于 \_\_ 个主题使用的标签”将是一个很好的改进，我们会非常需要它。

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [2020 年8 月 6 日 05:53 UTC](https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979/2 "2020-08-06T05:53:41Z")

</div>

更进一步，我可以设想一个设置，能够根据以下标准自动清理和删除标签：

```plaintext
count < N && topic_last_updated > X 个月前

```

基本上，让标签有机会站稳脚跟。如果有新主题在超时前发布，它们的寿命就会得到延续。但如果超过一定时间（例如 3 个月）没有新主题发布，且该标签关联的主题少于 5 个，那就直接将其删除。

---

<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: [2020 年8 月 7 日 04:33 UTC](https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979/3 "2020-08-07T04:33:22Z")

</div>

我也认为这些是改进标签清理功能的好建议 @neil

---

<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: [2020 年8 月 7 日 14:43 UTC](https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979/4 "2020-08-07T14:43:11Z")

</div>

我也从未见过这个按钮，而且我同意自动清理功能会很有用。它也应该小心不要删除仅限员工使用的标签。可能还有其他情况，某些标签也不应被自动删除。

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [2020 年8 月 9 日 21:36 UTC](https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979/5 "2020-08-09T21:36:24Z")

</div>

在此期间，这里有一个 #plugin:data-explorer 查询，可以帮助大家识别待删除的候选标签：

```plaintext
-- [params]
-- int :months_since_used = 24
-- int :max_topic_count = 50

with
t as (
  select 
    current_date::timestamp - (:months_since_used * (INTERVAL '1 months')) as cutoff_date
),
topic_tag_dates as (
  select tags.id, tags.name, tags.topic_count, topics.last_posted_at as last_used
  from topic_tags
  left join tags
  on topic_tags.tag_id = tags.id
  left join topics
  on topic_tags.topic_id = topics.id
),
max_last_used as(
  select id, max(last_used) mx from topic_tag_dates
  group by id
),
tag_last_used as (
  select topic_tag_dates.id, name, topic_count, last_used from topic_tag_dates
  left join max_last_used
  on topic_tag_dates.id = max_last_used.id
  where max_last_used.mx = topic_tag_dates.last_used
)
select id,name,topic_count,last_used from tag_last_used, t
  where tag_last_used.last_used < t.cutoff_date
  and topic_count < :max_topic_count
  order by topic_count desc

```

---

<div class="post-metadata">

### Author: ![anon89147131](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@anon89147131](https://meta.discourse.org/u/anon89147131)
#### Post date: [2020 年10 月 27 日 17:01 UTC](https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979/6 "2020-10-27T17:01:25Z")

</div>

抱歉再次顶贴，

是的，这个功能非常必要。我论坛上的有些人觉得开玩笑很有趣，发布了一些不太适合所有年龄段的“有趣”标签，因此我希望能看到这类功能。

谢谢。

---

<div class="post-metadata">

### Author: ![satonotdead](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/satonotdead/32/447830_2.png) [@satonotdead](https://meta.discourse.org/u/satonotdead)
#### Post date: [2023 年4 月 18 日 15:16 UTC](https://meta.discourse.org/t/ability-to-delete-tags-with-n-topics/159979/7 "2023-04-18T15:16:29Z")

</div>

那么如何运行它来删除 _topics = 1_ 的内容？

因为默认的清理程序只会删除没有主题的标签。_我们正在培训我们的员工，并且我限制了新内容的创建。_
