数千の個人メッセージを削除する方法?

私たちのコミュニティでは、ユーザー間で数千件のプライベートメッセージがやり取りされています。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.wherewhere("posts_count > 100") で拡張する必要があります)

「いいね!」 6