# Every record that matches

`limit: every` reads every record that matches, up to 10,000. Use it when
a request means all of them: "export all my todos", "clear all my
completed todos". If more than 10,000 match, the query gives back none of
them and the action stops, so a list is never cut short without anyone
knowing.

`domains/planning/queries/done_todos.jadpo`:

```jadpo
use planning/entities { Todo, User }
use planning/types { TodoStatus }

/// Every one of the owner's todos that is done, oldest first.
query done_todos(owner_id: User.id) -> List<Todo> {
    from: Todo
    where: Todo.owner_id == owner_id
    where: Todo.status == TodoStatus.done
    order: [Todo.created_at asc]
    limit: every
}
```

A change to every record that matches is that query and a loop of writes
in one action. `clear_done` deletes all of the owner's done todos and
says how many it deleted. The query runs before anything is written, and
the action is all-or-nothing, so when too many match, nothing is deleted:

`domains/planning/actions/clear_done.jadpo`:

```jadpo
use planning/entities { Todo, User }
use planning/queries { done_todos }

/// How many todos a clear deleted.
type ClearedCount = Int {}

/// Deletes every one of the owner's done todos, and gives how many.
action clear_done(owner_id: User.id) -> ClearedCount {
    var done: List<Todo> = done_todos(owner_id)
    for todo in done {
        Todo.delete(todo)
    }
    return ClearedCount(list.count(done))
}
```

```jadpo
use planning/entities { Todo, User }
use planning/types { TodoStatus }
use planning/actions { add_todo, add_user }
use planning/queries { done_todos }

test "clearing deletes every done todo of the owner's, and nobody else's" {
    var ada: User = add_user("Ada")
    var bob: User = add_user("Bob")
    var milk: Todo = add_todo(ada, "Buy milk", TodoStatus.done)
    var bread: Todo = add_todo(ada, "Buy bread", TodoStatus.done)
    var bins: Todo = add_todo(ada, "Put the bins out", TodoStatus.open)
    var bobs: Todo = add_todo(bob, "Walk the dog", TodoStatus.done)
    assert list.map(done_todos(ada.id), todo -> todo.id) == [milk.id, bread.id]
    assert clear_done(ada.id) == 2
    assert done_todos(ada.id) == []
    assert list.map(done_todos(bob.id), todo -> todo.id) == [bobs.id]
    assert bins.status == TodoStatus.open
}
```

The statement asks for one more than 10,000, so the runtime knows when
more match:

```sql
-- done_todos
SELECT "id", "owner_id", "title", "status", "created_at"
FROM "todo"
WHERE "owner_id" = $1
  AND "status" = $2
ORDER BY "created_at" ASC, "id" ASC
LIMIT 10001
```

When more than 10,000 match, the action ends as a
[defect](/docs/faults.md#defects) does: the client gets 500
`internal_fault`, nothing the action wrote is kept, and the log names the
query's call with the classification `too_many_records`. It is a fault
rather than a failure in `fails`, so a call of the query takes no
`attempt`.

The maximum is the same in every app and every deployment, so an action
behaves the same in tests as in production; it isn't a setting. Where a
set can grow past it, read the records a page at a time
([Pages](/docs/order-and-pages.md#pages)), or walk them in a [batch](/docs/batches-work-over-many-items.md),
which a large export is.

`limit: every` goes on a query that returns a `List<T>`, a map layer or a
list of groups. A page's `size:`, an include's `limit:` and a list ordered
by `nearest` each take a number
([`query.every_unsupported`](/docs/errors/query.every_unsupported.md)).
