Nossa comunidade tem vários milhares de mensagens privadas que foram enviadas entre usuários. Existe alguma maneira de remover completamente mensagens privadas com mais de 100 respostas do banco de dados (sem recuperação)?
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.
You can remove all message at once.
rake destroy:private_messages
Reference:
Nice work, @IAmGav! I swear i looked there and didn’t see.
Thanks Jay,
Running batch delete query directly on the database is so Scary ![]()
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
Revisitando este tópico antigo, já que nenhuma das respostas está 100% correta.
A tarefa rake rake destroy:private_messages não exclui permanentemente todas as mensagens privadas, ela as exclui logicamente. Elas ainda podem ser vistas por um administrador e podem ser recuperadas.
O código Rails Topic.where(archetype: 'private_message').where("user_id > 0").destroy_all destrói os tópicos, mas não as mensagens reais. Elas ainda estão no banco de dados e podem ser visualizadas usando, por exemplo, o plugin data explorer.
Este código irá excluir permanentemente todas as postagens e tópicos de mensagens privadas:
aviso: faca afiada aqui!
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
(e para filtrar isso em tópicos com 100 respostas ou mais, você precisa estender o Topic.where com where("posts_count > 100") em ambas as linhas)