Delete all posts and topics

AI-generated summary

A user, Aryeh_Armon, wants to delete all posts and topics from their Discourse instance to re-import data while preserving site settings like design and site name. zogstrip explains that the only reliable way to start fresh is to reset the database using rails db:drop db:create db:migrate, but this deletes all data including settings. To preserve settings, zogstrip suggests manually backing up the site_settings table using pg_dump, though the user initially encounters an error with the command. zogstrip clarifies the correct syntax. The user also expresses concern about losing 200,000 users, but zogstrip notes there’s no easy way to back up users. Later, simon points to a new rake task for deleting topics, which may help, though it’s not directly applicable to the user’s full request.

I have imported about 500,000 posts to my discourse instance.
i would like to delete all posts and topics to re-import all my posts.

does anyone have any idea how to do this?

If you want to start from scratch, I would use the rails console to re-create a new database

rails db:drop db:create db:migrate

:warning: THIS WILL DELETE EVERYTHING (users, site settings, etc..) :warning:

the thing is i want to keep all the other settings i have.

What other settings?

design, site name… and other settings my client may have changed.
i don’t see why i would have to reset everything.
there gotta be another way.

In that case, you could do this in the rails console

Post.order(id: :desc).each do |post| 
   PostDestroyer.new(Discourse.system_user, post).destroy
end

this is working wayy to slow. it deleted about 10000 posts in about 1 hour.
is there anyway to do this faster?

Only the first method I mentioned will. If you use anything else, you will end up with an inconsistent database.

is there a way to backup only the settings?

I’m afraid not yet. But you can do it manually. All the settings (ie. /admin/site_settings) are stored in the site_settings table. Just dump that table and restore it once you’ve re-created the database.

i do not come from the rails world,
is it to much to ask how i can do that?

something like this ?
rake db:site_settings:dump

It’s actually a postgresql thing :wink: Here’s how to dump the site_settings table

pg_dump -a -t site_settings -f site_settings.sql discourse_development

/usr/lib/postgresql/9.3/bin/pg_dump: invalid option – ‘D’
Try “pg_dump --help” for more information.

You can remove that -D parameter. Sorry.

just remembered i have 200,000 users i would like to keep as well :slight_smile: anyway to easily export those?

I’m afraid there is none…

OK, thank you so much. you helped me a lot.

There is now a rake task for this: