Skip to content

Instantly share code, notes, and snippets.

@jackyvo
Last active April 5, 2022 05:01
Show Gist options
  • Save jackyvo/fc7266c1e864d6c082995cd0f56e219e to your computer and use it in GitHub Desktop.
Save jackyvo/fc7266c1e864d6c082995cd0f56e219e to your computer and use it in GitHub Desktop.
Users destroy
# lib/tasks/users.rake
namespace :users do
task spam_clean: :environment do
failed_list = []
no_users_without_enrollments = 0
number_of_thread = (ENV['THREADS'] || 10).to_i
total_users = User.count.to_f
offset = (total_users / number_of_thread).ceil
id = User.unscoped.order(id: :asc).first.id
theads = []
number_of_thread.times.each do |i|
users = User.unscoped.order(id: :asc).where("id > ?", id).limit(offset)
theads << Thread.new do
puts "Start thread #{i+1}; #{users.count}"
users.find_each do |user|
if user.enrollments.empty?
no_users_without_enrollments += 1
begin
delete_user user
rescue Exception => ex
failed_list << user.id
end
end
end
end
id = users.last.id
end
theads.each do |thread|
thread.join
end
p "Total users to delete: #{no_users_without_enrollments}"
p "Total users that not deleted: #{failed_list.length}"
debug_list(failed_list)
end
def debug_list failed_list
#########################################
# Debug failed reason from list of failed
failed_list.each do |user_id|
begin
user = User.find_by(id: user_id)
user.delete
rescue Exception => ex
p "#{user_id}: => #{ex.message}"
end
end
end
def delete_user user
user.destroy
user.eportfolios.each do |port|
port.eportfolio_entries.delete_all
port.eportfolio_categories.delete_all
end
user.account_users.destroy_all
user.account_users.unscope(where: :workflow_state).delete_all
user.profile.destroy
UserMergeData.where(user_id: user.id).each do |um|
UserMergeDataRecord.where(user_merge_data_id: um.id).destroy_all
UserMergeDataRecord.where(user_merge_data_id: um.id).delete_all
um.destroy
end
UserMergeData.unscoped.where(user_id: user.id).delete_all
UserMergeDataItem.where(user_id: user.id).destroy_all
UserMergeDataItem.where(user_id: user.id).delete_all
Submission.unscoped.where(user_id: user.id).destroy_all
user.web_conference_participants.destroy_all
WebConference.where(user_id: user.id).each do |w|
w.web_conference_participants.destroy_all
w.delete
end
user.user_preference_values.destroy_all
user.access_tokens.delete_all
WikiPage.where(user_id: user.id).destroy_all
WikiPage.unscoped.where(user_id: user.id).delete_all
PageComment.where(user_id: user.id).destroy_all
PageComment.unscoped.where(user_id: user.id).delete_all
DiscussionTopicParticipant.where(user_id: user.id).destroy_all
DiscussionTopicParticipant.unscoped.where(user_id: user.id).delete_all
Auditors::ActiveRecord::CourseRecord.where(user_id: user.id).destroy_all
Auditors::ActiveRecord::CourseRecord.unscoped.where(user_id: user.id).delete_all
DiscussionTopic.unscoped.where(user_id: user.id).each do |dt|
ActiveRecord::Base.connection.execute("delete from discussion_topic_materialized_views where discussion_topic_id = #{dt.id}")
ActiveRecord::Base.connection.execute("delete from discussion_topic_participants where discussion_topic_id = #{dt.id}")
dt.discussion_entries.each do |de|
DiscussionEntryParticipant.unscoped.where(discussion_entry_id: de.id).destroy_all
DiscussionEntryParticipant.unscoped.where(discussion_entry_id: de.id).delete_all
de.delete
end
end
DiscussionTopic.unscoped.where(user_id: user.id).delete_all
CommunicationChannel.where(user_id: user.id).each do |c|
c.notification_policies.destroy_all
c.delete
end
user.eportfolios.delete_all
user.pseudonyms.each do |ps|
ps.auditor_authentication_records.destroy_all
ps.auditor_authentication_records.delete_all
ps.session_persistence_tokens.destroy_all
end
user.pseudonyms.delete_all
user.communication_channels.delete_all
user.delete
end
end
@jackyvo
Copy link
Author

jackyvo commented Apr 5, 2022

THREADS=10 RAILS_ENV=production bundle exec rake users:spam_clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment