Bulk deactivate users with SQL query

I need deactivate users who has fake email.

I mentioned here:

https://meta.discourse.org/t/i-imported-migrated-many-users-with-bad-old-invalid-email-addresses/88813

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?

1 Like

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
2 Likes