Skip to content
This repository

delete_all should respect a limit scope #4979

daveyeu opened this Issue February 09, 2012 · 8 comments

7 participants

David Yeu Aaron Patterson Arun Agrawal John Pignata Ali Jason Noble Francesco Rodríguez
David Yeu

Hi there,

So we nearly just hosed a table in production by trying:


Thankfully, we decided to read up on how delete_all works here and tested it locally, since it turns out that this simply deletes everything in the table.

Now I get that a LIMIT clause doesn't make sense on a DELETE op, but can we add a sensible translation in this case? I would expect it to behave like .destroy_all, which appears to instantiate the records and call #destroy on each. A #delete_all wouldn't have to inflate AR objects here, but instead, could simply query for the ids to delete, and then perform a DELETE FROM ... id IN (...) query.

Thoughts? I think we can prevent some future pain here.


Dave & John

Arun Agrawal

Rails Version?

John Pignata

We see the behavior using both Rails 3.1.3 and 3.0.11.

Arun Agrawal

have you tried this?

Mode.delete_all(:limit => 100)

Arun Agrawal

Sorry it will not work. It expects as a column.



I was going to have a crack at implementing this and naively thought that "DELETE FROM foos LIMIT " was valid SQL. However it appears to only work in MySQL, or as compile-time option for sqlite. No mention of it after a cursory glance at pg docs.

@daveyeu , your idea of getting the ids and then doing a "DELETE FROM foos WHERE id in (...)" I think would do what you'd expect Foo.limit(10).delete_all. It would be interesting to hear what rails contributors think.

Jason Noble

Perhaps delete_all should raise an error if a limit is provided?

/cc @tenderlove

Aaron Patterson

@jasonnoble that seems acceptable to me.

Francesco Rodríguez

@tenderlove Any news? Maybe something like this:

def delete_all(conditions = nil)
  raise"delete_all doesn't support limit scope") if self.limit_value

  if conditions

@daveyeu @arunagw Is destroy_all method serve that function?

Aaron Patterson tenderlove closed this in 2f68125 May 01, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.