> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lancedb.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Updating and Modifying Table Data

> Learn how to update, merge, and delete rows in a LanceDB table.

Updating or modifying data involves changing rows in an existing table.
LanceDB provides two families of write operations that can modify data in a table:

* `update(...)`: mutate existing rows that match a SQL filter.
* `merge_insert(...)`: compare incoming rows to existing rows by key, then choose what to do for each case.

The `update` method is simpler to use when you already know which rows you want to modify and you do not need to compare against an incoming dataset. The `merge_insert` method is more powerful when you have a new dataset that you want to merge into an existing table, and you want LanceDB to handle the logic of comparing against existing rows by key.

Let's look at an example that demonstrates these operations in practice.

## Connect to LanceDB

Connect to your local LanceDB instance:

import lancedb

db = lancedb.connect("./data")


const db = await lancedb.connect("./data");


let db = connect("./data").execute().await.unwrap();


Or, connect to LanceDB Enterprise:

import lancedb

db = lancedb.connect(
&#x20;   uri="db://your-project-slug",
&#x20;   api\_key="your-api-key",
&#x20;   region="us-east-1",
)


const db = await lancedb.connect("db://your-project-slug", \{
&#x20; apiKey: "your-api-key",
&#x20; region: "us-east-1",
});


let uri = "db://your-project-slug";
let api\_key = "your-api-key";
let region = "us-east-1";


In the Rust snippets, a `make_users_reader` helper is used  to build Arrow input data.

fn make\_users\_reader(
&#x20;   ids: Vec\<i64>,
&#x20;   names: Vec\<\&str>,
&#x20;   login\_counts: Option\<Vec\<i64>>,
) -> Box\<dyn RecordBatchReader + Send> \{
&#x20;   let mut fields = vec!\[
&#x20;       Field::new("id", DataType::Int64, false),
&#x20;       Field::new("name", DataType::Utf8, false),
&#x20;   ];
&#x20;   let mut columns: Vec\<Arc\<dyn Array>> =
&#x20;       vec!\[Arc::new(Int64Array::from(ids)), Arc::new(StringArray::from(names))];

&#x20;   if let Some(login\_counts) = login\_counts \{
&#x20;       fields.push(Field::new("login\_count", DataType::Int64, true));
&#x20;       columns.push(Arc::new(Int64Array::from(login\_counts)));
&#x20;   }

&#x20;   let schema = Arc::new(Schema::new(fields));
&#x20;   let batch = RecordBatch::try\_new(schema.clone(), columns).unwrap();
&#x20;   let reader = RecordBatchIterator::new(vec!\[Ok(batch)].into\_iter(), schema);
&#x20;   Box::new(reader)
}


## Create the example table

We'll start by creating a simple table with `id`, `name`, and `login_count` columns. All examples below use the same table.

import pyarrow as pa

table = db.create\_table(
&#x20;   "users\_example",
&#x20;   data=pa.table(
&#x20;       \{
&#x20;           "id": \[1, 2],
&#x20;           "name": \["Alice", "Bob"],
&#x20;           "login\_count": \[10, 20],
&#x20;       }
&#x20;   ),
&#x20;   mode="overwrite",
)


const table = await db.createTable(
&#x20; "users\_example",
&#x20; \[
&#x20;   \{ id: 1, name: "Alice", login\_count: 10 },
&#x20;   \{ id: 2, name: "Bob", login\_count: 20 },
&#x20; ],
&#x20; \{ mode: "overwrite" },
);


let table = db
&#x20;   .create\_table(
&#x20;       "users\_example",
&#x20;       make\_users\_reader(vec!\[1, 2], vec!\["Alice", "Bob"], Some(vec!\[10, 20])),
&#x20;   )
&#x20;   .mode(CreateTableMode::Overwrite)
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 1 | Alice | 10 |
| 2 | Bob | 20 |

The example above shows a PyArrow schema. You can just as well create the table using other
table creation patterns (Pandas, Polars, Pydantic, iterators, etc.) -- see the [ingestion](https://docs.lancedb.com/tables/create.md) guide for more details.

## Choose a write method

| Family | Method | Use this when... |
| - | - | - |
| `update` | `update(where=..., values=...)` | You want to edit rows that already exist, using a SQL filter. |
| `merge_insert` | `.when_matched_update_all()` | You have incoming rows and want to update keys that already exist in the table. |
| `merge_insert` | `.when_not_matched_insert_all()` | You have incoming rows and want to insert keys that do not exist yet. |
| `merge_insert` | `.when_matched_update_all()` + `.when_not_matched_insert_all()` | You want both behaviors together (often called **upsert**: update existing keys **and** insert missing keys in the same operation). |
| `merge_insert` | `.when_not_matched_by_source_delete(...)` | You want to remove target rows that are missing from the incoming source set. |

Write operations return status metadata. For example, `update` reports updated row count and
committed `version`, `delete` reports deleted row count and `version`, and `merge_insert` reports
inserted, updated, deleted, retry-attempt, and `version` fields. Writes require a mutable table
handle; after checking out an older version for reads, call `checkout_latest` / `checkoutLatest`
before modifying data.
The committed `version` advances even for writes that affect zero rows, such as a delete predicate
that matches nothing.

## Update rows

Use `update` when you already know which target rows to modify and you do not need to compare against an incoming dataset.

import pyarrow as pa

table = db.create\_table(
&#x20;   "users\_example",
&#x20;   data=pa.table(
&#x20;       \{
&#x20;           "id": \[1, 2],
&#x20;           "name": \["Alice", "Bob"],
&#x20;           "login\_count": \[10, 20],
&#x20;       }
&#x20;   ),
&#x20;   mode="overwrite",
)
table.update(where="id = 2", values=\{"name": "Bobby"})


const table = await db.createTable(
&#x20; "users\_example",
&#x20; \[
&#x20;   \{ id: 1, name: "Alice", login\_count: 10 },
&#x20;   \{ id: 2, name: "Bob", login\_count: 20 },
&#x20; ],
&#x20; \{ mode: "overwrite" },
);
await table.update(\{ where: "id = 2", values: \{ name: "Bobby" } });


let table = db
&#x20;   .create\_table(
&#x20;       "users\_example",
&#x20;       make\_users\_reader(vec!\[1, 2], vec!\["Alice", "Bob"], Some(vec!\[10, 20])),
&#x20;   )
&#x20;   .mode(CreateTableMode::Overwrite)
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();
table
&#x20;   .update()
&#x20;   .only\_if("id = 2")
&#x20;   .column("name", "'Bobby'")
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 1 | Alice | 10 |
| 2 | Bobby | 20 |

> [!WARNING]
>
> Updating nested columns is not yet supported.

## Update rows with SQL expressions

Use `values_sql` when you want to use SQL-like expressions to update rows. This is useful for operations like incrementing a counter, or setting a column value based on another column.

import pyarrow as pa

table = db.create\_table(
&#x20;   "users\_example",
&#x20;   data=pa.table(
&#x20;       \{
&#x20;           "id": \[1, 2],
&#x20;           "name": \["Alice", "Bob"],
&#x20;           "login\_count": \[10, 20],
&#x20;       }
&#x20;   ),
&#x20;   mode="overwrite",
)
table.update(where="id = 2", values\_sql=\{"login\_count": "login\_count + 1"})


const table = await db.createTable(
&#x20; "users\_example",
&#x20; \[
&#x20;   \{ id: 1, name: "Alice", login\_count: 10 },
&#x20;   \{ id: 2, name: "Bob", login\_count: 20 },
&#x20; ],
&#x20; \{ mode: "overwrite" },
);
await table.update(\{
&#x20; where: "id = 2",
&#x20; valuesSql: \{ login\_count: "login\_count + 1" },
});


let table = db
&#x20;   .create\_table(
&#x20;       "users\_example",
&#x20;       make\_users\_reader(vec!\[1, 2], vec!\["Alice", "Bob"], Some(vec!\[10, 20])),
&#x20;   )
&#x20;   .mode(CreateTableMode::Overwrite)
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();
table
&#x20;   .update()
&#x20;   .only\_if("id = 2")
&#x20;   .column("login\_count", "login\_count + 1")
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 1 | Alice | 10 |
| 2 | Bob | 21 |

> [!NOTE]
>
> See the [SQL queries](https://docs.lancedb.com/search/sql.md) page for more information on the supported SQL syntax.

When rows are updated, they are moved out of any existing index. The row will still show up in search queries, but the query will not be as fast as it would be if the row was in the index. If you update a large proportion of rows, consider triggering an index rebuild afterwards.

## Merge incoming rows by key

Merging is different from updating because it involves comparing incoming rows to existing rows by key, and then choosing what to do based on whether the key exists in the target table or not.
The `merge_insert(""..."")` method lets you do this.

In merge operations, rows are split into three groups:

* **Matched**: key exists in both source and target.
* **Not matched**: key exists only in source.
* **Not matched by source**: key exists only in target.

Conditional merge clauses can compare old and new values. Use the `target.` prefix for the
existing table row and `source.` for the incoming row, for example
`target.last_update < source.last_update`.

> [!TIP]
>
> **Use scalar indexes to speed up merge insert**
>
> The merge insert command performs a join between the input data and the target table `on` the key you provide. This requires scanning that entire column, which can be expensive for large tables. To speed up this operation, create a scalar index on the join column, which will allow LanceDB to find matches without scanning the whole table.
>
> Read more about scalar indices in the [Scalar Index](https://docs.lancedb.com/indexing/scalar-index.md) guide.

> [!NOTE]
>
> If you see this HTTP 400 error from `merge_insert`: `Bad request: Merge insert cannot be performed because the number of unindexed rows exceeds the maximum of 10000`. Verify that the scalar index on the join column is up to date before retrying.

> [!NOTE]
>
> **Rust: build merge predicates with DataFusion expressions**
>
> In the Rust SDK, you can pass a `datafusion_expr::Expr` directly instead of a SQL string by using
> `when_matched_update_all_expr` and `when_not_matched_by_source_delete_expr` on `MergeInsertBuilder`.
> This is useful when you are constructing predicates programmatically and want to avoid building a SQL string.
>
> These methods are only supported on local tables. Calling them against a remote table returns a
> `NotSupported` error — use the SQL string variants (`when_matched_update_all` /
> `when_not_matched_by_source_delete`) for remote tables.

Like the create table and add APIs, the merge insert API will automatically compute embeddings based on the [embedding registry](https://docs.lancedb.com/embedding/index.md#embedding-registry) if the table has an embedding definition in its schema.

During `merge_insert`, if the input data doesn't contain the source column (i.e., the original field used to generate embeddings, such as text for a text embedding model or `image_uri` for an image model), or if a vector value is already provided, LanceDB skips embedding generation for that row. Embeddings are only auto-generated when that source field is present in the incoming data, **and** the vector field is empty.

Primary keys in LanceDB are metadata used by operations such as `merge_insert`; they are not
enforced as uniqueness constraints on ordinary writes. Keep using `merge_insert` or your own
deduplication logic when you need key-based upsert semantics.

### Update matched rows only

This updates keys that already exist in the target table. Source rows with new keys are ignored.

import pyarrow as pa

table = db.create\_table(
&#x20;   "users\_example",
&#x20;   data=pa.table(
&#x20;       \{
&#x20;           "id": \[1, 2],
&#x20;           "name": \["Alice", "Bob"],
&#x20;           "login\_count": \[10, 20],
&#x20;       }
&#x20;   ),
&#x20;   mode="overwrite",
)

incoming\_users = pa.table(
&#x20;   \{
&#x20;       "id": \[2, 3],
&#x20;       "name": \["Bobby", "Charlie"],
&#x20;       "login\_count": \[21, 5],
&#x20;   }
)

(table.merge\_insert("id").when\_matched\_update\_all().execute(incoming\_users))


const table = await db.createTable(
&#x20; "users\_example",
&#x20; \[
&#x20;   \{ id: 1, name: "Alice", login\_count: 10 },
&#x20;   \{ id: 2, name: "Bob", login\_count: 20 },
&#x20; ],
&#x20; \{ mode: "overwrite" },
);

const incomingUsers = \[
&#x20; \{ id: 2, name: "Bobby", login\_count: 21 },
&#x20; \{ id: 3, name: "Charlie", login\_count: 5 },
];

await table
&#x20; .mergeInsert("id")
&#x20; .whenMatchedUpdateAll()
&#x20; .execute(incomingUsers);


let table = db
&#x20;   .create\_table(
&#x20;       "users\_example",
&#x20;       make\_users\_reader(vec!\[1, 2], vec!\["Alice", "Bob"], Some(vec!\[10, 20])),
&#x20;   )
&#x20;   .mode(CreateTableMode::Overwrite)
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();

let mut merge\_insert = table.merge\_insert(&\["id"]);
merge\_insert.when\_matched\_update\_all(None);
merge\_insert
&#x20;   .execute(make\_users\_reader(
&#x20;       vec!\[2, 3],
&#x20;       vec!\["Bobby", "Charlie"],
&#x20;       Some(vec!\[21, 5]),
&#x20;   ))
&#x20;   .await
&#x20;   .unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 1 | Alice | 10 |
| 2 | Bobby | 21 |

### Insert unmatched rows only

This inserts only brand-new keys from the source. Existing keys are left unchanged.

import pyarrow as pa

table = db.create\_table(
&#x20;   "users\_example",
&#x20;   data=pa.table(
&#x20;       \{
&#x20;           "id": \[1, 2],
&#x20;           "name": \["Alice", "Bob"],
&#x20;           "login\_count": \[10, 20],
&#x20;       }
&#x20;   ),
&#x20;   mode="overwrite",
)

incoming\_users = pa.table(
&#x20;   \{
&#x20;       "id": \[2, 3],
&#x20;       "name": \["Bobby", "Charlie"],
&#x20;       "login\_count": \[21, 5],
&#x20;   }
)

(table.merge\_insert("id").when\_not\_matched\_insert\_all().execute(incoming\_users))


const table = await db.createTable(
&#x20; "users\_example",
&#x20; \[
&#x20;   \{ id: 1, name: "Alice", login\_count: 10 },
&#x20;   \{ id: 2, name: "Bob", login\_count: 20 },
&#x20; ],
&#x20; \{ mode: "overwrite" },
);

const incomingUsers = \[
&#x20; \{ id: 2, name: "Bobby", login\_count: 21 },
&#x20; \{ id: 3, name: "Charlie", login\_count: 5 },
];

await table
&#x20; .mergeInsert("id")
&#x20; .whenNotMatchedInsertAll()
&#x20; .execute(incomingUsers);


let table = db
&#x20;   .create\_table(
&#x20;       "users\_example",
&#x20;       make\_users\_reader(vec!\[1, 2], vec!\["Alice", "Bob"], Some(vec!\[10, 20])),
&#x20;   )
&#x20;   .mode(CreateTableMode::Overwrite)
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();

let mut merge\_insert = table.merge\_insert(&\["id"]);
merge\_insert.when\_not\_matched\_insert\_all();
merge\_insert
&#x20;   .execute(make\_users\_reader(
&#x20;       vec!\[2, 3],
&#x20;       vec!\["Bobby", "Charlie"],
&#x20;       Some(vec!\[21, 5]),
&#x20;   ))
&#x20;   .await
&#x20;   .unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 1 | Alice | 10 |
| 2 | Bob | 20 |
| 3 | Charlie | 5 |

### Update matched rows and insert unmatched rows

Use both `when_matched_update_all()` and `when_not_matched_insert_all()` when you want to update existing keys and insert missing keys in one operation.

> [!NOTE]
>
> This is a conventional **upsert**.

import pyarrow as pa

table = db.create\_table(
&#x20;   "users\_example",
&#x20;   data=pa.table(
&#x20;       \{
&#x20;           "id": \[1, 2],
&#x20;           "name": \["Alice", "Bob"],
&#x20;           "login\_count": \[10, 20],
&#x20;       }
&#x20;   ),
&#x20;   mode="overwrite",
)

incoming\_users = pa.table(
&#x20;   \{
&#x20;       "id": \[2, 3],
&#x20;       "name": \["Bobby", "Charlie"],
&#x20;       "login\_count": \[21, 5],
&#x20;   }
)

(
&#x20;   table.merge\_insert("id")
&#x20;   .when\_matched\_update\_all()
&#x20;   .when\_not\_matched\_insert\_all()
&#x20;   .execute(incoming\_users)
)


const table = await db.createTable(
&#x20; "users\_example",
&#x20; \[
&#x20;   \{ id: 1, name: "Alice", login\_count: 10 },
&#x20;   \{ id: 2, name: "Bob", login\_count: 20 },
&#x20; ],
&#x20; \{ mode: "overwrite" },
);

const incomingUsers = \[
&#x20; \{ id: 2, name: "Bobby", login\_count: 21 },
&#x20; \{ id: 3, name: "Charlie", login\_count: 5 },
];

await table
&#x20; .mergeInsert("id")
&#x20; .whenMatchedUpdateAll()
&#x20; .whenNotMatchedInsertAll()
&#x20; .execute(incomingUsers);


let table = db
&#x20;   .create\_table(
&#x20;       "users\_example",
&#x20;       make\_users\_reader(vec!\[1, 2], vec!\["Alice", "Bob"], Some(vec!\[10, 20])),
&#x20;   )
&#x20;   .mode(CreateTableMode::Overwrite)
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();

let mut merge\_insert = table.merge\_insert(&\["id"]);
merge\_insert
&#x20;   .when\_matched\_update\_all(None)
&#x20;   .when\_not\_matched\_insert\_all();
merge\_insert
&#x20;   .execute(make\_users\_reader(
&#x20;       vec!\[2, 3],
&#x20;       vec!\["Bobby", "Charlie"],
&#x20;       Some(vec!\[21, 5]),
&#x20;   ))
&#x20;   .await
&#x20;   .unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 1 | Alice | 10 |
| 2 | Bobby | 21 |
| 3 | Charlie | 5 |

### Delete target rows that are missing from source

Use `when_not_matched_by_source_delete()` when you want to remove any target row that does not appear in the incoming source data.

import pyarrow as pa

table = db.create\_table(
&#x20;   "users\_example",
&#x20;   data=pa.table(
&#x20;       \{
&#x20;           "id": \[1, 2, 3],
&#x20;           "name": \["Alice", "Bob", "Charlie"],
&#x20;           "login\_count": \[10, 20, 5],
&#x20;       }
&#x20;   ),
&#x20;   mode="overwrite",
)

incoming\_users = pa.table(
&#x20;   \{
&#x20;       "id": \[2, 3],
&#x20;       "name": \["Bobby", "Charlie"],
&#x20;       "login\_count": \[21, 5],
&#x20;   }
)

(
&#x20;   table.merge\_insert("id")
&#x20;   .when\_matched\_update\_all()
&#x20;   .when\_not\_matched\_insert\_all()
&#x20;   .when\_not\_matched\_by\_source\_delete()
&#x20;   .execute(incoming\_users)
)


const table = await db.createTable(
&#x20; "users\_example",
&#x20; \[
&#x20;   \{ id: 1, name: "Alice", login\_count: 10 },
&#x20;   \{ id: 2, name: "Bob", login\_count: 20 },
&#x20;   \{ id: 3, name: "Charlie", login\_count: 5 },
&#x20; ],
&#x20; \{ mode: "overwrite" },
);

const incomingUsers = \[
&#x20; \{ id: 2, name: "Bobby", login\_count: 21 },
&#x20; \{ id: 3, name: "Charlie", login\_count: 5 },
];

await table
&#x20; .mergeInsert("id")
&#x20; .whenMatchedUpdateAll()
&#x20; .whenNotMatchedInsertAll()
&#x20; .whenNotMatchedBySourceDelete()
&#x20; .execute(incomingUsers);


let table = db
&#x20;   .create\_table(
&#x20;       "users\_example",
&#x20;       make\_users\_reader(
&#x20;           vec!\[1, 2, 3],
&#x20;           vec!\["Alice", "Bob", "Charlie"],
&#x20;           Some(vec!\[10, 20, 5]),
&#x20;       ),
&#x20;   )
&#x20;   .mode(CreateTableMode::Overwrite)
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();

let mut merge\_insert = table.merge\_insert(&\["id"]);
merge\_insert
&#x20;   .when\_matched\_update\_all(None)
&#x20;   .when\_not\_matched\_insert\_all()
&#x20;   .when\_not\_matched\_by\_source\_delete(None);
merge\_insert
&#x20;   .execute(make\_users\_reader(
&#x20;       vec!\[2, 3],
&#x20;       vec!\["Bobby", "Charlie"],
&#x20;       Some(vec!\[21, 5]),
&#x20;   ))
&#x20;   .await
&#x20;   .unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 2 | Bobby | 21 |
| 3 | Charlie | 5 |

In the example above, LanceDB matches rows by `id`. Rows with `id=2` and `id=3` exist in both the table and incoming data, so they are updated. Row `id=1` exists only in the target, so it is deleted.

### Use partial columns in merge updates

Merge updates do not require you to provide values for all columns. You can provide only a subset of columns in source rows. For matched rows, only the provided columns are updated.

import pyarrow as pa

table = db.create\_table(
&#x20;   "users\_example",
&#x20;   data=pa.table(
&#x20;       \{
&#x20;           "id": \[1, 2],
&#x20;           "name": \["Alice", "Bob"],
&#x20;           "login\_count": \[10, 20],
&#x20;       }
&#x20;   ),
&#x20;   mode="overwrite",
)

incoming\_users = pa.table(
&#x20;   \{
&#x20;       "id": \[2, 3],
&#x20;       "name": \["Bobby", "Charlie"],
&#x20;   }
)

(
&#x20;   table.merge\_insert("id")
&#x20;   .when\_matched\_update\_all()
&#x20;   .when\_not\_matched\_insert\_all()
&#x20;   .execute(incoming\_users)
)


const table = await db.createTable(
&#x20; "users\_example",
&#x20; \[
&#x20;   \{ id: 1, name: "Alice", login\_count: 10 },
&#x20;   \{ id: 2, name: "Bob", login\_count: 20 },
&#x20; ],
&#x20; \{ mode: "overwrite" },
);

const incomingUsers = \[
&#x20; \{ id: 2, name: "Bobby" },
&#x20; \{ id: 3, name: "Charlie" },
];

await table
&#x20; .mergeInsert("id")
&#x20; .whenMatchedUpdateAll()
&#x20; .whenNotMatchedInsertAll()
&#x20; .execute(incomingUsers);


let table = db
&#x20;   .create\_table(
&#x20;       "users\_example",
&#x20;       make\_users\_reader(vec!\[1, 2], vec!\["Alice", "Bob"], Some(vec!\[10, 20])),
&#x20;   )
&#x20;   .mode(CreateTableMode::Overwrite)
&#x20;   .execute()
&#x20;   .await
&#x20;   .unwrap();

let mut merge\_insert = table.merge\_insert(&\["id"]);
merge\_insert
&#x20;   .when\_matched\_update\_all(None)
&#x20;   .when\_not\_matched\_insert\_all();
merge\_insert
&#x20;   .execute(make\_users\_reader(vec!\[2, 3], vec!\["Bobby", "Charlie"], None))
&#x20;   .await
&#x20;   .unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 1 | Alice | 10 |
| 2 | Bobby | 20 |
| 3 | Charlie | null |

Note that in the example above, when `merge_insert` creates a new row, any missing columns are written as `null`. If a missing column is non-nullable in your schema, the insert will fail.

## Delete rows

Delete operations **soft delete** rows that match a given condition.
The underlying data is not immediately removed, but is marked
for deletion (in the [deletion files](https://lance.org/format/table/#deletion-files) at the Lance format level) and excluded from query results.

\# delete data
predicate = "id = 3"
table.delete(predicate)


// delete data
const predicate = "id = 3";
await table.delete(predicate);


// delete data
let predicate = "id = 3";
table.delete(predicate).await.unwrap();


Expected table contents:

| id | name | login\_count |
| - | - | - |
| 1 | Alice | 10 |
| 2 | Bob | 20 |

> [!WARNING]
>
> **Deleting rows removes them from the index**
>
> When rows are deleted, those rows are also excluded from the index segments, so indexed queries will not return them either. If ALL the rows are deleted (i.e., the table is emptied), ensure that you recreate the index after ingesting new data.

To permanently remove deleted rows, you can optimize the table, which will run compaction and cleans up the soft-deleted rows, which frees up storage space.

* In LanceDB OSS, compaction and cleanup are manual. Run `table.optimize()` regularly to free up disk space.
* In LanceDB Enterprise, files aren't cleaned up by default. You can configure automatic compaction and cleanup behavior at cluster setup time to suit your organization's retention policy.

By default, table cleanup removes data up to 7 days ago. If you need to reclaim space from deleted rows more aggressively, manually call `table.optimize()` use a shorter retention window as follows:

from datetime import timedelta

table.optimize(cleanup\_older\_than=timedelta(days=1))


const olderThan = new Date();
olderThan.setDate(olderThan.getDate() - 1);
await table.optimize(\{ cleanupOlderThan: olderThan });


table
&#x20;   .optimize(OptimizeAction::Prune \{
&#x20;       older\_than: Some(Duration::days(1)),
&#x20;       delete\_unverified: None,
&#x20;       error\_if\_tagged\_old\_versions: None,
&#x20;   })
&#x20;   .await
&#x20;   .unwrap();


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.