# 通过 Rails 完全删除数百个主题的正确方法？

**URL:** <https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323>\
**Category:** Support\
**Created:** [2018年六月20日 15:52 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323 "2018-06-20T15:52:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [2018年六月20日 15:52 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/1 "2018-06-20T15:52:40Z")

</div>

Hi.  
I would like to delete ALL topics from a single category (~5000 topics) in order to re-import these topics from phpbb.  
I could delete these topics, but since Discourse doesn’t really delete them but hide them, my phpbb import won’t re-import these messages.  
I can’t just re-import from scratch either because I did a lot of configuration since my import. Not only discourse admin config but also reorganized categories, moved hundreds of topics by keywords, filtered a lot of messages with regexes to remove some content, edited some users and so on.

I was suggested a rails command. Note that my forum isn’t active opened to my community yet. I imported phpbb messages but no users logged in or posted on the forum. I hope that may minimize the probability that something is broken after the deletion.

Would:

```ruby
Topic.where(category_id: XX).destroy_all

```

do the trick?  
Do you think there some database tables that should be cleaned up in some way after this?

Any idea, advice suggestion to achieve this?

---

<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:** [2018年六月20日 22:58 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/2 "2018-06-20T22:58:48Z")

</div>

I’m not sure – this might also be performance intensive depending on how many weirdo Rails ActiveRecord things this causes to cascade through. Any thoughts @neil have you ever done this?

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2018年六月21日 05:18 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/3 "2018-06-21T05:18:58Z")

</div>

I would do this in batches of say 10, building up to batches of 200 depending on how long it takes.

```plaintext
Topic.where(category_id: XX).limit(20).destroy_all

```

It does cascade all stuff it needs so it could take a while.

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [2018年六月21日 09:06 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/4 "2018-06-21T09:06:48Z")

</div>

Hi,  
I’ve done this without setting a limit and it properly removed all the 5000 topics, but it didn’t seem to destroy all the posts from these topics as the posts table still had the same number of records.

Plus, maybe there is a lot of related data in other table to take care of?

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2018年六月21日 10:28 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/5 "2018-06-21T10:28:07Z")

</div>

Interesting… we are missing a dependant destroy here, I am not sure why.

> <https://github.com/discourse/discourse/blob/main/app/models/topic.rb#L119>

Easy to fix though

```ruby
Post.where('topic_id not in (select id from topics)').limit(100).destroy_all

```

And repeat… till no results.

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [2018年六月21日 10:58 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/6 "2018-06-21T10:58:21Z")

</div>

Thanks for your reply 🙂

Do you see any table which can contain potentially annoying residual data or do you think that would do the trick and it would be a safe enough method to definitively destroy my topics?

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2018年六月21日 11:15 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/7 "2018-06-21T11:15:37Z")

</div>

I can not guarantee anything 🙂 you are in uncharted territory so you will need to walk all the tables and audit it

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [2018年六月21日 11:28 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/8 "2018-06-21T11:28:02Z")

</div>

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)\
**Post date:** [2018年六月21日 11:58 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/9 "2018-06-21T11:58:12Z")

</div>

Good bit of history here:  
 ![26](https://global.discourse-cdn.com/meta/optimized/3X/3/3/336dd9717d587f73be4f7ed8a6682bfb014ba9bd_2_690x15.png)

---

<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:** [2018年七月6日 12:38 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/10 "2018-07-06T12:38:53Z")

</div>

> [@Canapin](#):
>
> I could delete these topics, but since Discourse doesn’t really delete them but hide them, my phpbb import won’t re-import these messages.

Make sure you also delete the `import_id` custom fields on posts (topics?) if you want to ensure the importer re-import them.

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [2018年七月6日 14:20 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/11 "2018-07-06T14:20:18Z")

</div>

Hi,  
Finally, I ended not having to delete my topics, I was able to do what I wanted to (not necessarily deleting them ; this could have been a solution but not the only one), see [Migrate a phpBB3 forum to Discourse - #367 by Canapin](https://meta.discourse.org/t/importing-from-phpbb3/30810/367), [Migrate a phpBB3 forum to Discourse - #377 by Canapin](https://meta.discourse.org/t/importing-from-phpbb3/30810/377) and [Migrate a phpBB3 forum to Discourse - #379 by Canapin](https://meta.discourse.org/t/importing-from-phpbb3/30810/379)

---

<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:** [2021年一月30日 04:05 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/12 "2021-01-30T04:05:50Z")

</div>



---

<div class="post-metadata">

**Author:** ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)\
**Post date:** [2021年二月5日 08:31 UTC](https://meta.discourse.org/t/the-proper-way-to-completely-delete-hundred-of-topics-via-rails/90323/13 "2021-02-05T08:31:09Z")

</div>


