حذف جميع الرسائل من مستخدم في فئة

Hi guys,

Recently I had the need to delete all posts from a user in a specific category. Leaving aside “debates” on deleting content I thought “oh, easy, just use the search and then pick all the results using the checkboxes and delete them”.

However, it seems that doing so allows me to delete The Topics and not so much the messages, even though the search is giving me the messages as results, not necessarily the topics (although they are there as well, yes).

Is there a “safe” way of doing this? At first I thought that I would be something very extreme to the DB (and I don’t discard that possibility) but since I can bulk-delete 50+ topics from the UI… maybe there is a way of doing that for messages only? (Or a way using the console, I just don’t want to “try” and blow up the DB :stuck_out_tongue: )

Thanks!

إعجاب واحد (1)

I don’t have personal experience of these, but I believe you can ‘soft’ delete using the API -

And there are a few topics on ‘hard’ deleting if you search for ‘rails delete post’.

If you can give a few more details about what you’re trying to achieve, people may have more specific tips.

Hi @JammyDodger

Thanks for the links.

I just want to delete all messages from a user in a specific category. :stuck_out_tongue:

PD: Maybe using the Console? I just don’t want to nuke my DB…

إعجاب واحد (1)

There’s some ongoing discussion about a similar thing in this topic: Delete deleted-posts permanently in bulk?

Hopefully that can provide some inspiration?

نعتذر عن النيكرو، سعيد بإنشاء منشور جديد إذا لزم الأمر.

لدينا فئة السياسة وبعض المستخدمين يرغبون في حذف جميع منشوراتهم من هذه الفئة لأسباب واضحة. هل هناك أي طريقة للقيام بذلك بسهولة نيابة عنهم؟

يمكن أن يكون الحل هو استخدام وحدة تحكم Rais.

cd /var/discourse
./launcher enter app
rails c

بعد ذلك، يمكنك اتباع النموذج التالي عن طريق ملء slug الفئة و اسم المستخدم.
يمكنك نسخ الكود ولصقه في وحدة التحكم.

ملاحظة:

  • لا يحذف المنشور الأول لتجنب حذف الموضوع
  • إذا كنت تستهدف فئة فرعية، فقم بتوفير الفئة الأم، على سبيل المثال: parent_cat_slug/cat_slug.

:warning: يرجى عمل نسخة احتياطية قبل إجراء أي تغييرات.

Category
  .find_by_slug_path_with_id("=CATEGORY_SLUG=")
  .tap do |c|
    u = User.find_by_username("=USERNAME=")
    Post
      .joins(:topic)
      .where(user_id: u.id)
      .where("topics.category_id = ?", c.id)
      .where.not(post_number: 1)
      .where(deleted_at: nil)
      .find_each { |p| PostDestroyer.new(Discourse.system_user, p).destroy }
  end
إعجاب واحد (1)

شكراً! هذا رائع.