Gently delete huge user accounts with background task

I use Rails and develop an application that stores a lot of user data. I want the user to be able to deactivate their account, similar to how Facebook does this, so that when you log in, your account will be activated again. So far I have been doing this with a soft delete. The problem is that when people delete their accounts, there is so much data that needs to be gently deleted that it takes some time to start. Naturally, my instinct was to use delayed_job for this. But the problem is that this only works when deleting an account, and not during reactivation. I do not want my users to sit for 10 seconds while all their data has been restored, but I also can not do this in the background, because then they will be returned before any of their data is restored.

Any ideas on how to solve this problem?

Thanks in advance!

+4
source share
1 answer

You want your soft_delete to be tracked as a boolean flag in all relevant entries. Set default areas to return only records that do not have the flag set. When the time comes to activate or deactivate, collect all your relevant entries and click them with update_all . Here, the example starts with 13,000 user records to give you a sense of time and performance:

 1.9.2p320 :001 > User.update_all(soft_deleted: false) SQL (1016.3ms) UPDATE "users" SET "soft_deleted" = 'f' => 13350 

As you can see, he hit all 13,000 records so that this flag switches for about one second. So, if you want to hit the user, all messages of the user and all users of PrivateMessages,

 User.update_attributes(soft_deleted: true) User.posts.update_all(soft_deleted: true) User.private_messages.update_all(soft_deleted: true) 

And you should be good to go. If you deal with so many entries that even this technique doesn’t work well, I don’t think you will have much choice but to tell the user what might be a few minutes before all their data is available for reactivation and throw the whole process into a background task as you originally planned.

+2
source

All Articles