Note: nowadays there is a specific class for destroying Users and associated records cleanly: UserDestroyer.
So instead of this:
This should be done:
destroyer = UserDestroyer.new(Discourse.system_user)
User.joins(:user_stat).where("user_stats.post_count = 0").each { |u| destroyer.destroy(u) }