> For the complete documentation index, see [llms.txt](https://crystal-garage.gitbook.io/lustra-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://crystal-garage.gitbook.io/lustra-docs/model/batchs-operations/bulk-update.md).

# Bulk update

Use `update_all` to update every row matched by a collection without loading models.

```crystal
affected = User.query
  .where(active: false)
  .update_all(active: true)

puts "Updated #{affected} users"
```

`update_all` returns the number of affected rows.

```crystal
affected = User.query
  .where { id.in?([1, 2]) }
  .update_all(first_name: "Updated", last_name: "User")
```

You can pass a named tuple or a string-keyed hash.

```crystal
User.query.where(active: false).update_all({active: true})

fields = {} of String => Lustra::SQL::Any
fields["active"] = true
User.query.where(active: false).update_all(fields)
```

`update_all` bypasses validations, callbacks, change tracking, and automatic timestamp updates. Use it only when direct SQL behavior is what you want.

## Custom Update Queries

Use `to_update` when you need lower-level update builder features.

```crystal
User.query
  .where(active: false)
  .to_update
  .set(last_seen_at: Lustra::SQL.unsafe("NOW()"))
  .execute_and_count
```

`to_update` copies the collection's table and `WHERE` clauses into a `Lustra::SQL::UpdateQuery`.

## Bulk Delete

Use `delete_all` to delete every row matched by a collection without loading models.

```crystal
User.query.where(active: false).delete_all
```

`delete_all` bypasses destroy callbacks.

Use `destroy_all` when callbacks must run.

```crystal
User.query.where(active: false).destroy_all
```

## Custom Delete Queries

Use `to_delete` when you need the lower-level delete builder.

```crystal
User.query
  .where(active: false)
  .to_delete
  .execute_and_count
```

`to_delete` copies the collection's table and `WHERE` clauses into a `Lustra::SQL::DeleteQuery`.

## Limitations

`to_update` and `to_delete` require a query with exactly one source table. They are not meant for collections built from multiple tables or subqueries.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://crystal-garage.gitbook.io/lustra-docs/model/batchs-operations/bulk-update.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
