How to delete thousands of Private Messages?

Revisiting this old topic, since none of the answers are 100% correct.

The rake task rake destroy:private_messages does not hard delete all private messages, it soft deletes them. They can still be seen by an admin and they can be recovered.

The Rails code Topic.where(archetype: 'private_message').where("user_id > 0").destroy_all destroys the topics, but not the actual messages. They are still in the database and can be viewed using for instance the data explorer plugin.

This code will permanently delete all private message posts and topics:
warning: sharp knife here!

Post.where(topic_id: Topic.where(archetype: 'private_message').where("user_id > 0")).destroy_all
Topic.where(archetype: 'private_message').where("user_id > 0").destroy_all

(and to filter this on topics with 100 replies or more you need to extend the Topic.where with where("posts_count > 100") in both lines)

6 Likes