The proper way to completely delete hundred of topics via rails?

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.


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?

1 Like

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?


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

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

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


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?

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

Easy to fix though

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

And repeat… till no results.


Thanks for your reply :slight_smile:

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?

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

1 Like

Thank you for your help!

Good bit of history here:


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


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 Importing from phpBB3, Importing from phpBB3 and Importing from phpBB3

1 Like