> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Change the Oplog Size of Self-Managed Replica Set Members

**Warning:**

You cannot drop the `local.oplog.rs` collection. For more information on this restriction, see [Oplog Collection Behavior.](https://www.mongodb.com/docs/manual/core/replica-set-oplog.md#std-label-oplog-coll-behavior)

This procedure changes the size of the oplog  on each member of a replica set using the [`replSetResizeOplog`](https://www.mongodb.com/docs/manual/reference/command/replsetresizeoplog.md#mongodb-dbcommand-dbcmd.replSetResizeOplog) command, starting with the [secondary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-secondary) members before proceeding to the [primary.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary)

Perform these steps on each [secondary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-secondary) replica set member *first*. Once you have changed the oplog size for all secondary members, perform these steps on the [primary.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary)

## A. Connect to the replica set member

Connect to the replica set member using [`mongosh`:](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh)

```bash
mongosh --host <hostname>:<port>
```

**Note:**

If the replica set enforces [authentication](https://www.mongodb.com/docs/manual/core/authentication.md#std-label-authentication), you must authenticate as a user with privileges to modify the `local` database, such as the [`clusterManager`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-clusterManager) or [`clusterAdmin`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-clusterAdmin) role.

## B. (Optional) Verify the current size of the oplog

To view the current size of the oplog, switch to the `local` database and run [`db.collection.stats()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.stats.md#mongodb-method-db.collection.stats) against the `oplog.rs` collection. [`stats()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.stats.md#mongodb-method-db.collection.stats) displays the oplog size as [`maxSize`.](https://www.mongodb.com/docs/manual/reference/command/collstats.md#mongodb-data-collStats.maxSize)

```javascript
use local
db.oplog.rs.stats().maxSize
```

The `maxSize` field displays the collection size in bytes.

## C. Change the oplog size of the replica set member

Resize the oplog with the [`replSetResizeOplog`](https://www.mongodb.com/docs/manual/reference/command/replsetresizeoplog.md#mongodb-dbcommand-dbcmd.replSetResizeOplog) command. The `size` is a double and must be greater than `990` megabytes. To explicitly cast the oplog `size` in [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh), use the `Double()` constructor.

The following operation changes the oplog size of the replica set member to 16 gigabytes, or 16000 megabytes.

```javascript
db.adminCommand({replSetResizeOplog: 1, size: Double(16000)})
```

The oplog can grow past its configured size limit to avoid deleting the [`majority commit point`.](https://www.mongodb.com/docs/manual/reference/command/replsetgetstatus.md#mongodb-data-replSetGetStatus.optimes.lastCommittedOpTime)

## D. (Optional) Compact `oplog.rs` to reclaim disk space

Reducing the size of the oplog does *not* automatically reclaim the disk space allocated to the original oplog size. You must run [`compact`](https://www.mongodb.com/docs/manual/reference/command/compact.md#mongodb-dbcommand-dbcmd.compact) against the `oplog.rs` collection in the `local` database to reclaim disk space. There are no benefits to running `compact` on the `oplog.rs` collection after increasing the oplog size.

**Important:**

A replica set member cannot replicate oplog entries when a `compact` operation is ongoing on `oplog.rs` because it prevents oplog synchronization. You must schedule `compact` operations on the oplog during a maintenance window. Oplog replication cannot occur during that time window.

Do **not** run `compact` against the primary replica set member. Connect a [`mongo`](https://www.mongodb.com/docs/manual/reference/mongo.md#mongodb-binary-bin.mongo) shell directly to the primary (not the replica set) and run [`rs.stepDown()`](https://www.mongodb.com/docs/manual/reference/method/rs.stepdown.md#mongodb-method-rs.stepDown). If successful, the primary steps down. From the [`mongo`](https://www.mongodb.com/docs/manual/reference/mongo.md#mongodb-binary-bin.mongo) shell, run the `compact` command on the now-secondary member.

The following operation runs the `compact` command against the `oplog.rs` collection:

```javascript
use local
db.runCommand({ "compact" : "oplog.rs" } )
```

For clusters enforcing [authentication](https://www.mongodb.com/docs/manual/core/authentication.md#std-label-authentication), authenticate as a user with the [`compact`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-compact) privilege action on the `local` database and the `oplog.rs` collection. For complete documentation on [`compact`](https://www.mongodb.com/docs/manual/reference/command/compact.md#mongodb-dbcommand-dbcmd.compact) authentication requirements, see [`compact` Required Privileges.](https://www.mongodb.com/docs/manual/reference/command/compact.md#std-label-compact-authentication)
