# file.update()

Canonical URL: https://developers.iagenify.com/sdk/ref/files/update/
Language: en

`file.update() → Promise<Result>`

Replaces the content of a file already saved. It takes no arguments: it sends whatever is on the object now.

Status: PUT /api/files/:id · free · needs a sourceId

The unusual part is that it reads the object rather than its arguments. Set `file.content`, call `update()`, and that is the new content.

```javascript
const file = await api.files.get(sourceId);

file.content = "# Q4\n\nRevised after the audit.";
await file.update();
```

## What it needs {#requires}

| Condition | Otherwise |
| --- | --- |
| file.sourceId is set | the SDK throws: there is nothing to update |
| file.content is not null | the SDK throws: an empty file is not a content |

Both are thrown locally, before any request. A file comes with its `sourceId` from [get()](/sdk/ref/files/get), or acquires it on [save()](/sdk/ref/files/save) — so the natural sequence works and a freshly `create()`d object does not.

```javascript
const file = api.files.create({ name: "notes.md", content: "…" });
await file.update();   // throws — never saved, so no sourceId

await file.save();     // now it has one
file.content = "…";
await file.update();   // fine
```

## Returns {#returns}

```javascript
{
  ok: true,
  message: "…",
  sourceId: "…",
  metadata: { size: 2048, mimeType: "text/markdown" }
}
```

## It replaces, it does not append {#replaces}

The new content is the whole content. To add to a file, read it, concatenate, and update.

```javascript
const file = await api.files.get(sourceId);
file.content = `${file.content}\n${newLine}`;
await file.update();
```

> **There is no previous version**
>
> The old content is gone. Read it first if it mattered — reading is free. A file whose history matters wants a new file per version, not an update.

## What it does not change {#immutable}

| Changed | Unchanged |
| --- | --- |
| The content, and its size | The name |
| The storage's remaining space | The mimeType, and so the category |
|  | The sourceId |

So a text file cannot become an image by updating it. Save a new one for that.

## Searchable text is not re-indexed {#indexing}

> **Semantic search still answers from the old text**
>
> Indexing happens on save. After an update, a search can return the version you replaced. Where that matters, delete and save again rather than update.

## Errors {#errors}

| Code | Status | When |
| --- | --- | --- |
| — (thrown locally) | — | No sourceId, or no content. |
| file_not_found | 404 | No file of this account with that id. |
| missing_content | 400 | Nothing usable reached the server. |

- [get()](/sdk/ref/files/get): Reading one, with its sourceId already set.
- [delete()](/sdk/ref/files/delete): Removing it, and what was indexed from it.

```python
file = api.files.get(source_id)

file.content = "# Q4\n\nRevised after the audit."
file.update()
```

```python
file = api.files.create(name="notes.md", content="…")
file.update()   # raises ValueError — never saved, so no source_id

file.save()     # now it has one
file.content = "…"
file.update()   # fine
```

```python
{
    "ok": True,
    "message": "…",
    "sourceId": "…",
    "metadata": {"size": 2048, "mimeType": "text/markdown"},
}
```

```python
file = api.files.get(source_id)
file.content = f"{file.content}\n{new_line}"
file.update()
```
