- Reference >
- Database Commands >
- Sharding Commands >
- cleanupOrphaned
cleanupOrphaned¶
On this page
Definition¶
-
cleanupOrphaned¶ New in version 2.6.
Deletes from a shard the orphaned documents whose shard key values fall into a single or a single contiguous range that do not belong to the shard. For example, if two contiguous ranges do not belong to the shard, the
cleanupOrphanedexamines both ranges for orphaned documents.To run, issue
cleanupOrphanedin theadmindatabase directly on themongodinstance that is the primary replica set member of the shard. You do not need to disable the balancer before runningcleanupOrphaned.Note
Do not run
cleanupOrphanedon amongosinstance.cleanupOrphanedhas the following syntax:cleanupOrphanedhas the following fields:Field Type Description cleanupOrphanedstring The namespace, i.e. both the database and the collection name, of the sharded collection for which to clean the orphaned data. startingFromKeydocument Optional. The shard key value that determines the lower bound of the cleanup range. The default value is
MinKey.If the range that contains the specified
startingFromKeyvalue belongs to a chunk owned by the shard,cleanupOrphanedcontinues to examine the next ranges until it finds a range not owned by the shard. See Determine Range for details.secondaryThrottleboolean Optional. If
true, each delete operation must be replicated to another secondary before the cleanup operation proceeds further. Iffalse, do not wait for replication. Defaults tofalse.Independent of the
secondaryThrottlesetting, after the final delete,cleanupOrphanedwaits for all deletes to replicate to a majority of replica set members before returning.writeConcerndocument Optional. A document that expresses the write concern that the
secondaryThrottlewill use to wait for the secondaries when removing orphaned data.writeConcernrequiressecondaryThrottle: true.
Behavior¶
Performance¶
cleanupOrphaned scans the documents in the shard to
determine whether the documents belong to the shard. As such, running
cleanupOrphaned can impact performance; however,
performance will depend on the number of orphaned documents in the
range.
To remove all orphaned documents in a shard, you can run the command in a loop (see Remove All Orphaned Documents from a Shard for an example). If concerned about the performance impact of this operation, you may prefer to include a pause in-between iterations.
Alternatively, to mitigate the impact of cleanupOrphaned,
you may prefer to run the command at off peak hours.
Determine Range¶
The cleanupOrphaned command uses the startingFromKey
value, if specified, to determine the start of the range to examine for
orphaned document:
- If the
startingFromKeyvalue falls into a range for a chunk not owned by the shard,cleanupOrphanedbegins examining at the start of this range, which may not necessarily be thestartingFromKey. - If the
startingFromKeyvalue falls into a range for a chunk owned by the shard,cleanupOrphanedmoves onto the next range until it finds a range for a chunk not owned by the shard.
The cleanupOrphaned deletes orphaned documents from the
start of the determined range and ends at the start of the chunk range
that belongs to the shard.
Consider the following key space with documents distributed across
Shard A and Shard B.
Shard A owns:
Chunk 1with the range{ x: minKey } --> { x: -75 },Chunk 2with the range{ x: -75 } --> { x: 25 }, andChunk 4with the range{ x: 175 } --> { x: 200 }.
Shard B owns:
Chunk 3with the range{ x: 25 } --> { x: 175 }andChunk 5with the range{ x: 200 } --> { x: maxKey }.
If on Shard A, the cleanupOrphaned command runs with
startingFromKey: { x: -70 } or any other value belonging to range for
Chunk 1 or Chunk 2, the cleanupOrphaned command examines
the Chunk 3 range of { x: 25 } --> { x: 175 } to delete
orphaned data.
If on Shard B, the cleanupOrphaned command runs with
the startingFromKey: { x: -70 } or any other value belonging to range
for Chunk 1, the cleanupOrphaned command examines the
combined contiguous range for Chunk 1 and Chunk 2, namely {
x: minKey } --> { x: 25 } to delete orphaned data.
Required Access¶
On systems running with authorization, you must have
clusterAdmin privileges to run cleanupOrphaned.
Output¶
Return Document¶
Each cleanupOrphaned command returns a document containing
a subset of the following fields:
-
cleanupOrphaned.ok¶ Equal to
1on success.A value of
1indicates thatcleanupOrphanedscanned the specified shard key range, deleted any orphaned documents found in that range, and confirmed that all deletes replicated to a majority of the members of that shard’s replica set. If confirmation does not arrive within 1 hour,cleanupOrphanedtimes out.A value of
0could indicate either of two cases:cleanupOrphanedfound orphaned documents on the shard but could not delete them.cleanupOrphanedfound and deleted orphaned documents, but could not confirm replication before the 1 hour timeout. In this case, replication does occur but only aftercleanupOrphanedreturns.
-
cleanupOrphaned.stoppedAtKey¶ The upper bound of the cleanup range of shard keys. If present, the value corresponds to the lower bound of the next chunk on the shard. The absence of the field signifies that the cleanup range was the uppermost range for the shard.
Log Files¶
The cleanupOrphaned command prints the number of deleted
documents to the mongod log. For example:
Examples¶
The following examples run the cleanupOrphaned command
directly on the primary of the shard.
Remove Orphaned Documents for a Specific Range¶
For a sharded collection info in the test database, a shard
owns a single chunk with the range: { x: MinKey } --> { x: 10 }.
The shard also contains documents whose shard keys values fall in a
range for a chunk not owned by the shard: { x: 10 } --> { x: MaxKey
}.
To remove orphaned documents within the { x: 10 } => { x: MaxKey }
range, you can specify a startingFromKey with a value that falls into
this range, as in the following example:
Or you can specify a startingFromKey with a value that falls into the
previous range, as in the following:
Since { x: 2 } falls into a range that belongs to a chunk owned by
the shard, cleanupOrphaned examines the next range to find
a range not owned by the shard, in this case { x: 10 } => { x: MaxKey
}.
Remove All Orphaned Documents from a Shard¶
cleanupOrphaned examines documents from a single
contiguous range of shard keys. To remove all orphaned documents from
the shard, you can run cleanupOrphaned in a loop, using
the returned stoppedAtKey as the next startingFromKey, as in
the following: