Bulk deactivate users with SQL query


#1

I need deactivate users who has fake email.

I mentioned here:

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

I write SQL query for that:

User.where("user_stats.post_count = 0 AND last_seen_at <= '2017-01-01' AND active = true").update_all(active: false)

Output:

[1] pry(main)> User.joins(:user_stat).where("user_stats.post_count = 0 AND last_[1] pry(main)> User.joins(:user_stat).where("user_stats.post_count = 0 AND last_=> 4591 <= '2017-01-01' AND active = true").update_all(active: false)

It seems worked. Please backup your Discourse before run that SQL.

Do you have any better suggestions?


(Jay Pfaffman) #2

Seems fine. Here’s mine that just does pretty much everyone:

  def deactivate_all_users
    User.where("active = true and admin != true").update_all(active: false)
  end