Operations: Server: How to Compact a Database

To compact database, please use CompactDatabaseOperation. You can choose what should be compacted: documents and/or listed indexes.

Syntax

public CompactDatabaseOperation(CompactSettings compactSettings)

public class CompactSettings
{
    public string DatabaseName { get; set; }

    public bool Documents { get; set; }

    public string[] Indexes { get; set; }
}
Parameters
DatabaseName string Name of a database to compact
Documents bool Indicates if documents should be compacted
Indexes string[] List of index names to compact

Example I

CompactSettings settings = new CompactSettings
{
    DatabaseName = "Northwind",
    Documents = true,
    Indexes = new[] { "Orders/Totals", "Orders/ByCompany" }
};
Operation operation = store.Maintenance.Server.Send(new CompactDatabaseOperation(settings));
operation.WaitForCompletion();

Example II

// get all index names
string[] indexNames = store.Maintenance.Send(new GetIndexNamesOperation(0, int.MaxValue));

CompactSettings settings = new CompactSettings
{
    DatabaseName = "Northwind",
    Documents = true,
    Indexes = indexNames
};
// compact entire database: documents + all indexes
Operation operation = store.Maintenance.Server.Send(new CompactDatabaseOperation(settings));
operation.WaitForCompletion();

Remarks

The compacting operation is executed asynchronously and during this operation the database will be offline.