如何删除数千条个人消息?

我们的社区有数千条用户之间发送的私人消息。有没有办法从数据库中完全删除回复超过100条的私人消息(无法恢复)?

1 个赞

Well, I think that this will do what you asked. It will delete any private messages that were not created by a system user or discobot. It will still delete any other private messages, including those from admins.

It’s not tested. I’m not saying that it’s a good idea. I won’t promise that it won’t do something bad.

cd /var/discourse
discourse backup
./launcher enter app
rails s
Topic.where(archetype: 'private_message').where("user_id > 0").destroy_all
exit
discourse enable_restore
discourse restore

If something bad does not happen, you can omit the last two steps. If something bad does happen, you’ll want to copy/paste the most recent backup printed by the discourse restore command to restore the backup.

3 个赞

You can remove all message at once.

rake destroy:private_messages

Reference:

6 个赞

Nice work, @IAmGav! I swear i looked there and didn’t see.

3 个赞

Thanks Jay,
Running batch delete query directly on the database is so Scary :sweat_smile:

1 个赞

I took a brief look at how this function works. As it turns out, after running it, all private messages will be deleted.

  def destroy_private_messages
    Topic.where(archetype: "private_message").find_each do |pm|
      @io.puts "Destroying #{pm.slug} pm"
      first_post = pm.ordered_posts.first
      @io.puts PostDestroyer.new(Discourse.system_user, first_post).destroy
    end
  end

I think I have to choose the scary way that @pfaffman pointed out to filter messages. or try to define custom rake function and use PostDestroyer.new(Discourse.system_user, first_post).destroy

2 个赞

重新讨论这个旧话题,因为没有一个答案是100%正确的。

rake 任务 rake destroy:private_messages 不会硬删除所有私人消息,它只会软删除它们。管理员仍然可以看到它们,并且可以恢复它们。

Rails 代码 Topic.where(archetype: 'private_message').where("user_id > 0").destroy_all 会删除主题,但不会删除实际的消息。它们仍然在数据库中,可以使用例如数据浏览器插件来查看。

这段代码永久删除所有私人消息帖子和主题:
警告:这里有锋利的刀!

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

(要将此过滤到回复超过100条的主题,您需要在两行中将 Topic.where 扩展为 where("posts_count > 100")

6 个赞