# BGREWRITEAOF

> Rewrite the append-only file in the background so it holds the current data set compactly.

URL: https://docs.hotkv.com/commands/bgrewriteaof/

```
BGREWRITEAOF
```

- Group: Server
- Complexity: O(1) to start; the rewrite itself is O(N) in the number of keys
- HotKV Cloud: all plans
- On-Premise: no license needed

The append-only file (AOF) grows with every write. `BGREWRITEAOF` starts
rewriting it in the background as the shortest sequence of commands that
recreates the current data set, then replaces the old file. Clients keep
being served, and writes made during the rewrite are kept.

The server also rewrites the AOF on its own when it has grown by
`auto-aof-rewrite-percentage` since the last rewrite and is larger than
`auto-aof-rewrite-min-size`. Check progress with `aof_rewrite_in_progress`
and the result with `aof_last_bgrewrite_status` in [INFO](https://docs.hotkv.com/commands/info/index.md)
`persistence`.

## Return value

| Case | Reply (RESP2 and RESP3) |
| --- | --- |
| Rewrite started | Simple string `Background append only file rewriting started` |
| A rewrite is already running | Error `ERR Background append only file rewriting already in progress` |

## Examples

```
127.0.0.1:6379> BGREWRITEAOF
Background append only file rewriting started
```

In HotKV Cloud, the CLI tab of the panel does not run `BGREWRITEAOF`. Send
it from your own client connection.

## Compatibility

Same replies as Redis 7 and Valkey 8. HotKV's AOF file format is its own;
see [persistence](https://docs.hotkv.com/persistence/index.md).

## See also

[BGSAVE](https://docs.hotkv.com/commands/bgsave/index.md), [WAITAOF](https://docs.hotkv.com/commands/waitaof/index.md),
[CONFIG SET](https://docs.hotkv.com/commands/config-set/index.md) (`appendonly`, `appendfsync`)