Help Wiping all Posts from a Forum

Hi folks,

One of the Discourse sites that I operate is preparing to undergo a re-launch, and due to the large amount of inactivity on the forum, and the type of posts that have been made in the last year or so, I’d like to remove/delete all topics and start over fresh on the forums.

Is there an easy way to do this?

Thanks in advance!

2 个赞

Do you want to delete everything and start from scratch, or just the topics, categories, users?

Hi Jeff,

We really just want to delete the topics and categories. We’d like to keep users and all of the css work that has been done on the instance.

I don’t know that we have a simple way to do that at the moment, but it does come up a fair bit.

Can we create a rake task that would remove all categories and topics @techapj? Perhaps add this to your list as low priority?

5 个赞

Thanks Jeff! We’ll figure out something.

Hello,

Are there any updates considering an easy way of wiping out every thread-post-category in a discourse forum?

Thank you in advance.

The easiest way is to use rails command line, ssh into your server and then

cd /var/discourse
./launcher enter app
rails c
Post.where("user_id > 0").where(post_number: 1).find_each do |post| 
  PostDestroyer.new(Discourse.system_user, post).destroy
  putc "."
end

:warning: be sure to make a backup before running this destructive code.

7 个赞

Thanks a lot @zogstrip for the quick answer!

Can you suggest a quick/safe backup method?

Use the backup feature in Discourse :wink:

1 个赞

Oh, my bad. Had to check first before asking.

Thanks anyway! :wink:

Does running PostDestroyer.destroy actually remove the record from the database or does it merely mark the post as deleted, keeping it in the database?

There will be a lot of uses for a script that actually DELETES all the topics and posts, leaving everything (like site settings and users) there. Categories are yes/no.

It only marks them as deleted in the database.

That’s what I’m afraid of.

There must be some internal Discourse database guru who can whip up a bunch of SQL statements to just wipe the necessary tables clean…

What about being able to back up just users, and/or site settings, categories etc.
Then uploading to a fresh install?

Thanks to @blake we now have rake tasks for cleanup of various sorts .

8 个赞

How do these destroyed posts manifest in the UI?

What I’m looking to do:

Keep all site structure, (categories, groups, users, and settings), but wipe all threads, and PMS, and logs, basically to take a demo site that served as a sandbox, and transition it to a live site, similar to the OP, without having to setup a new instance and re-register all my users. But everything content wise, as well as logged actions all starts fresh.

Does this rake task posted above do this, or can the added reset logs as well be done, so all post data, and all log data is wiped fresh?

这真的很有用,非常感谢。看起来它并没有删除帖子的任何自定义字段。在我的情况下,我正在尝试导入帖子,因此“import_id”自定义字段仍然存在,导致导入器认为帖子仍然存在,即使它们实际上并不存在。有什么方法可以一并删除自定义字段数据吗? :wink:

当然,在 PostDestroyer.new 这一行之前添加以下代码:

post.custom_fields = nil
post.save_custom_fields
3 个赞