Bulk remove users

AI 生成的摘要

Users are discussing how to bulk remove inactive users in Discourse after importing data from phpBB3. yavuz initially imported 4,000 users with no posts and sought a way to delete them. zogstrip suggested using Rails console with User.where(post_count: 0).destroy_all, but this failed because post_count is now stored in the user_stats table. zogstrip corrected the query to User.joins(:user_stat).where("user_stats.post_count = 0").destroy_all, which yavuz confirmed works. jomaxro extended the query to also filter users inactive for over a year, which zogstrip approved. Queth asked how to delete all non-admin users before final import to prevent accidental emails, and leonardo recommended using UserDestroyer for safer, cleaner deletions: destroyer = UserDestroyer.new(Discourse.system_user); User.joins(:user_stat).where("user_stats.post_count = 0").each { |u| destroyer.destroy(u) }. Users are advised to always backup before running destructive queries.

I have imported my old data from phpBB3 which went smooth. But I made a mistake by not first cleaning up the old inactive users and still importing them. I now have around 4000 users with no posts I wish to remove.

What is the best way to bulk remove users?

If you really don’t care about these users, then I would do this

  1. make a backup (better safe than sorry)
  2. ssh into your server and type
cd /var/discourse
./launcher enter app
rails c
User.where(post_count: 0).destroy_all

These instructions produce the following error:
[1] pry(main)> User.where(post_count: 0).destroy_all ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column users.post_count does not exist LINE 1: SELECT "users".* FROM "users" WHERE "users"."post_count" = 0 ^ : SELECT "users".* FROM "users" WHERE "users"."post_count" = 0 from /var/www/discourse/vendor/bundle/ruby/2.3.0/gems/rack-mini-profiler-0.10.1/lib/patches/db/pg.rb:90:in `exec'

This is not really documented I think but if it is please point me in the right direction?

“post count” != “post_count” ??

My bad, the post_count property was moved to another table for optimization reasons.

Here’s the updated query

User.joins(:user_stat).where("user_stats.post_count = 0").destroy_all

Thank you, the updated query works!

One of my sites uses SSO and we’re looking to remove users that have never posted and haven’t been seen in a year (to avoid killing users who are just quiet). Is this query valid in the rails console?

User.joins(:user_stat).where("user_stats.post_count = 0 AND previous_visit_at <= '2016-05-20'::timestamp").destroy_all

It looks good to me.

我遇到了同样的问题,不过是在进行最终导入之前需要删除用户。我一直在尝试在导入之前先搭建好包含所有设置、主题等的基础环境——现在(经过大量工作后)我发现,我实际上是在一个部分导入的基础上操作的,该部分导入已经导入了 4000 名用户。

我希望删除这些用户,以免(在我忘记更改相关设置时)有人意外收到摘要邮件或其他通知。

我该如何调整该查询语句,使得仅保留管理员用户?
目前活跃用户列表中约有 4000 名用户,
暂停用户列表中约有 30 名用户。

提前感谢 :slight_smile:

注意:如今有一个专门用于干净地销毁用户和相关记录的类:UserDestroyer。

所以,不要这样做:

应该这样做:

destroyer = UserDestroyer.new(Discourse.system_user)
User.joins(:user_stat).where("user_stats.post_count = 0").each { |u| destroyer.destroy(u) }