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 Likes

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 Likes

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 Likes

Thanks a lot @zogstrip for the quick answer!

Can you suggest a quick/safe backup method?

Use the backup feature in Discourse :wink:

1 Like

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 .

https://github.com/discourse/discourse/blob/master/lib/tasks/destroy.rake

7 Likes

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?

This is really useful, many thanks. It seems that this doesn’t remove any Custom Fields for the posts. In my case, I am trying to import posts, so the “import_id” custom fields remain, making the importer think the posts are still present, even though they are not. Any way to remove the custom field data as well? :wink:

Sure, add these lines before the PostDestroyer.new line

post.custom_fields = nil
post.save_custom_fields
3 Likes