collimarco
collimarco

Reputation: 35400

How to destroy millions of dependent objects in Rails

In Rails I have always used dependent: :destroy to remove dependent objects.

However in the following scenario it doesn't work properly because Subscriptions and Notifications are very large, with millions of objects that must be destroyed.

Users --> Projects  --> Subscriptions
                   \
                    --> Notifications

Some problems are:

What is the Rails way to handle a large destroy with millions of records? Are there any gems that can help and handle the dependent: :destroy in a better way (e.g. outside a transaction)? What approach would you suggest in this case?

Upvotes: 1

Views: 557

Answers (1)

medBouzid
medBouzid

Reputation: 8402

Starting from Rails 6.1 it's possible to do dependent: :destroy_async to destroy associations in the background

 has_many :subscriptions, dependent: :destroy_async

just posted this for future reference

Upvotes: 3

Related Questions