I am getting 502 error while trying to delete topics created by specific user

While trying to delete topics created by specific user from admin pannel → users. Its showing me error 502.


Is there any other way to delete topic created by specfic user.

Hi @Yogesh_g,

You’re trying to delete all 301,719 posts created by this user? :grimacing: That seems like it will leave you with a lot of topics that don’t make sense because of the missing posts.

Would anonymizing the user make sense for you in this situation?

I think you’re getting this 502 error because of the “delete all posts max” setting.
image

3 Likes

I have increased the limit to greater than the number I want to delete and These topics only contains posts by the same user.

Can you please explain how anonymizing users works, would it make all posts hidden for all other users?

Is there any way I can access the database I am using discourse hosting ?

No, it doesn’t hide the posts; it removes the username. It’s a good solution at times when the user asks to have his account deleted, because it preserves the content, keeping the topics in your community intact, while making that content anonymous, so no one knows who wrote it. It is described fully, here:

If you have a hosted site, it might be best to let the Discourse team assist.

3 Likes

This is a pretty unique situation and a massive amount of [database] work - no wonder the request is timing out.

Sounds very exceptional; I’d also recommend contacting hosting support for assistance with this one.

3 Likes

Ok I will contact the support. And will update you guys here

Is there any other way to delete all posts for user. Or Can I try creating a custom plugin for it that delete post from database.( My client is asking for it but I am not sure that it will work for large number of posts as Discourse have the functionallity already).

Should I create a plugin for it or not. And If yes how it will work

@ Michael Brown @Southpaw I have created a plugin to delete posts of a user in batches of a given number. I have written some Ruby code to delete posts. Currently I am using settings api to take input from admin.

The challenge I am facing is that. I want to create a admin page for plugin that has a some input fields and a button. I want my Ruby code to run on click of that button.

I have tried someways but failed.

Need help, Is there any possible way to achieve it in discourse?

Hi @Yogesh_g,

:open_mouth:

I’m not (yet!) experienced in writing plugins, so I don’t have a quick answer for you on the questions you’ve asked about getting your admin page to work, but I’d like to make sure you’re getting the help you need.

I’m having some trouble putting together all the pieces here, so I’ve sent you a PM to get a little more detail.

Continued in How to execute server-side code when clicking a button?

Hi, discourse community @pfaffman @merefield

I need to delete all posts created by a particular user (a total of 301,719 posts ). I have tried various ways. but failed to delete.

How Should I delete all the posts for that user? Please provide a way. I am struggling with this for 3 months.

do you mean permanent delete or soft delete?

permanent delete. I don’t want these posts to be shown to any users.

Is this topic any use to you?

I don’t think it’s exactly what you’re after, but you may be able to amend it to target what you need?

This doesn’t work. I have tried it earlier

1 Like

I had a similar problem before, I needed to delete around 200000 posts from different users with lots of condition. Any ordinary method will bring long-term lag.
Finally I take the dumbest way. Loop from 1 to max post id. If it satisfy the condition, delete it. It took me about an hour to iterate through the them (about 900000 posts)

2 Likes

I tried a similer way the server gives time out error, below is the code

# frozen_string_literal: true

module Jobs
  class DeleteUserPosts < ::Jobs::Scheduled
    every 2.minutes

    def execute(args)
      return unless SiteSetting.delete_user_topics_enabled?

      username = SiteSetting.delete_posts_for_username
      posts_per_batch = SiteSetting.delete_posts_in_single_batch.to_i

      return unless username.present? && posts_per_batch.positive?

      user = User.find_by(username: username)
      return unless user.present?

      posts = user.posts.order(created_at: :asc)

      deleted_count = 0
      posts.each do |post|
        break if deleted_count >= posts_per_batch

        if SiteSetting.delete_user_topics_dry_run?
          Rails.logger.error("DeleteUserPosts would remove Post ID #{post.id} (#{post.topic.title} - #{post.excerpt}) (dry run mode)")
        else
          Rails.logger.error("DeleteUserPosts removing Post ID #{post.id} (#{post.topic.title} - #{post.excerpt})")
          begin
            PostDestroyer.new(Discourse.system_user, post).destroy
            deleted_count += 1
          rescue StandardError => e
            Rails.logger.error("Error deleting post ID #{post.id}: #{e.message}")
          end
        end
      end

      # Cancel the scheduled job if there are no more posts remaining
      if posts.size <= posts_per_batch
        self.class.cancel_scheduled_job
      end
    end
  end
end

This can obviously go wrong. 300000 posts is not a small number, you can’t load them into memory at once.

1 Like

Can please share the code snippet you used , or show an example