Why Ecto doesn't allow limit in delete_all?

Why Ecto doesn’t allow :limit in Repo.delete_all/2 ? It’s useful in some cases. It should be supported.

I have no idea why but if you’re looking for workarounds you might try something like:

query = from r in Review, select: r.id, limit: 10
review_ids = Repo.all(query)

query = from r in Review, where: r.id in ^review_ids

It’s not supported at all in Postgres:


Ecto shouldn’t have any trouble producing the subquery suggested there as a workaround.


In my case, I have a delete fun that runs everyday and the data can be really huge. I need to do it in batches to avoid table locks. Select and delete is a workaround but I’m looking for the most efficient solution.

I see. Yup looks good but looks like doing it in raw SQL with no select, just straight delete with limit will work best in my situation.

I think you’re already there, but query/4 is a nice escape hatch for your raw SQL.

1 Like

I think you’re looking for Repo.stream.


Yes this is what I used. I still think that Ecto should add support to :limit with Repo.delete_all/2 though :slight_smile:

1 Like