2

I have a Product model that has_many ProductAttributes in a Rails 4.2.3 application.

A Product can have over 10,000 ProducAttributes sometimes.

I need to delete some of these products, but when I call destroy on the product, it takes a really, really long time. It looks like it gets a list of ids from ProductAttribute, then called delete on each one individually.

But then when it gets to the end, it just sort of hangs. I have a lot of data to delete, and occasionally I will need to do this.

But I can't just have this thing hang forever. Eventually I have to do a Ctrl-C, after which time I see Rollback happening.

My question is, what in the heck is it doing? Why can't I reliably delete lots of data from the application layer?

RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193
  • Have you looked at [this possible dupe?](http://stackoverflow.com/questions/16680656/why-is-activerecord-destroy-all-taking-so-long) – jkeuhlen Jul 23 '15 at 20:42

1 Answers1

3

I suspect you use something like

has_many :product_attributes, dependent: :destroy

You might want to look into using dependent: :delete so Rails doesn't instantiate all dependent records before destroying them individually.

The documentation will have more information about consequences (e.g. if callbacks are executed on the dependent models etc.)

Community
  • 1
  • 1
awendt
  • 13,195
  • 5
  • 48
  • 66