The discussion is about pruning spam users from a Discourse forum. The goal is to remove users who haven’t logged in for a year and never posted. MakaryGo provided a Ruby on Rails command to delete these users: User.joins(:user_stat).where("user_stats.post_count = 0 AND previous_visit_at <= '2016-05-22'::timestamp").destroy_all.
Before running this command, codinghorror advised to batch the deletion to avoid timeouts and pfaffman suggested adding a limit clause to the query.
treb0r mentioned that the spam users were imported from BBpress and there might be around 5000 of them. pfaffman provided a way to get a count of users by adding a .count at the end of the command.
To avoid running these queries on the production forum, treb0r asked for guidance on maintaining a staged copy of the forum. JagWaugh provided a link to a guide on creating a sandbox, and JagWaugh and pfaffman offered tips on handling URLs and DNS in the sandbox.
treb0r successfully set up a sandbox and used the /etc/hosts file to redirect to the sandbox.
User.joins(:user_stat).where("user_stats.post_count = 0 AND previous_visit_at <= '2016-05-22'::timestamp").destroy_all
Should do the trick Of course make sure to make a backup first
Edit:
Sorry, forgot usage tips Of course you have to log into your server, go into the discourse directory [probably /var/discourse], enter your Docker container [probably ./launcher enter app] and then enter rails console by typing rails c . These are defaults on docker based installations @treb0r
Then you can try anything out in your sandbox, including checking that the latest OS patches and Discourse updates are ok. Our live site is on AWS, but if you run your sandbox on a different provider than your live site then you’ll be open to differences in OS/Droplet changes.
I don’t run any mail on the sandbox, so regardless of what I mess up it won’t start sending stuff to the users.
It depends. If you leave .com in .css, for example, then the sandbox will keep taking you to the live site.
iirc I just went through the settings and changed any that seemed might cause a problem, but to be absolutely safe you might want to change all of them.
Also, if you’re using S3 for storage, do something about that so that you are sure that you’re not influencing the live site.
Links to other posts inside the sandbox resolve to the corresponding item in the sandbox, but I think this might be dependent what you do with hostname in app.yml (if you copy your app.yml over from your live site then be veewy, veewy cawefuw…)