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

# Storage Methods

> Methods for persisting and loading embeddings

Methods for persisting and loading embeddings from storage.

## saveToStorage()

Save current state to storage.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
async saveToStorage(): Promise<void>
```

### Description

Persists all embeddings, metadata, and HNSW index structure (if enabled) to the configured storage adapter.

### Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
await db.addMany(documents);
await db.saveToStorage();
```

### Errors

| Error | Condition |
| - | - |
| `VectoriaNotInitializedError` | Database not initialized |
| `StorageError` | Storage operation failed |

***

## clearStorage()

Clear storage cache.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
async clearStorage(): Promise<void>
```

### Description

Deletes all persisted embeddings from storage. Does not affect in-memory data.

### Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Force re-index on next startup
await db.clearStorage();
```

***

## close()

Close the database and storage adapter.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
async close(): Promise<void>
```

### Description

Performs cleanup operations. Call when shutting down the application.

### Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Graceful shutdown
await db.saveToStorage();
await db.close();
```

***

## isInitialized()

Check if the database is initialized.

### Signature

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
isInitialized(): boolean
```

### Example

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
if (!db.isInitialized()) {
  await db.initialize();
}
```

## Persistence Pattern

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
import { VectoriaDB, FileStorageAdapter, SerializationUtils } from 'vectoriadb';

async function setupDB(documents: Document[]) {
  const db = new VectoriaDB({
    storageAdapter: new FileStorageAdapter({
      cacheDir: './.cache/vectoriadb',
    }),
    toolsHash: SerializationUtils.createToolsHash(documents),
    version: '1.0.0',
  });

  await db.initialize();

  // Check if cache was loaded
  if (db.size() === 0) {
    await db.addMany(documents);
    await db.saveToStorage();
  }

  return db;
}

// Graceful shutdown
process.on('SIGTERM', async () => {
  await db.saveToStorage();
  await db.close();
  process.exit(0);
});
```

## Error Handling

```ts theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
import { StorageError } from 'vectoriadb';

try {
  await db.saveToStorage();
} catch (error) {
  if (error instanceof StorageError) {
    console.warn('Storage failed, continuing in-memory:', error.message);
  }
}
```

## Related

> [card] [FileStorageAdapter](https://docs.agentfront.dev/vectoriadb/api-reference/storage-adapters/file-adapter.md)
>
> File storage

> [card] [RedisStorageAdapter](https://docs.agentfront.dev/vectoriadb/api-reference/storage-adapters/redis-adapter.md)
>
> Redis storage

> [card] [Storage Guide](https://docs.agentfront.dev/vectoriadb/guides/storage/overview.md)
>
> Storage guide


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