--- title: Clearing & Resetting description: Clear or reset MockServer state selectively by type (expectations, logs, or all) or by request matcher, with curl, Java, and JavaScript examples. layout: page pageOrder: 4 section: 'Data & State' subsection: true sitemap: priority: 0.7 changefreq: 'monthly' lastmod: 2019-11-10T08:00:00+01:00 ---

MockServer has the following internal state:

State can be cleared from MockServer selectively:

What each type affects: the type parameter is important for memory management. expectations clears only the configured expectations — it does not clear the recorded request / event log. On a long-running instance the request log grows with every request, so clearing expectations alone will not free that memory. To free memory held by accumulated log entries, use log or all, or perform a full reset with PUT /mockserver/reset.

Operation Expectations Request / event log
clear?type=expectations cleared kept
clear?type=log kept cleared
clear?type=all (the default when type is omitted) cleared cleared
PUT /mockserver/reset cleared cleared

Note: a request matcher or expectation id narrows clearing to matching items; reset always clears everything and ignores any matcher.

How clearing works: when logLevel is INFO or lower (the default), cleared log entries are soft-deleted rather than physically removed. This means:

If you want cleared log entries to be physically removed from memory, set the log level to WARN or higher.

new MockServerClient("localhost", 1080).clear(
    request(),
    ClearType.LOG
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .clear({}, 'LOG')
  .then(
    function () {
      console.log("cleared recorded requests and logs that matches request matcher");
    },
    function (error) {
      console.log(error);
    }
  );

See REST API for full JSON specification

from mockserver.client import MockServerClient

client = MockServerClient("localhost", 1080)
client.clear(clear_type="LOG")
require 'mockserver-client'

client = MockServer::Client.new('localhost', 1080)
client.clear(type: 'LOG')
import mockserver "github.com/mock-server/mockserver-monorepo/mockserver-client-go/v7"

client := mockserver.New("localhost", 1080)
client.Clear(nil, mockserver.ClearLog)
using MockServer.Client;

using var client = new MockServerClient("localhost", 1080);
client.Clear(type: "LOG");
use mockserver_client::{ClientBuilder, ClearType};

let client = ClientBuilder::new("localhost", 1080).build().unwrap();
client.clear(None, Some(ClearType::Log)).unwrap();
use MockServer\MockServerClient;

$client = new MockServerClient('localhost', 1080);
$client->clear(null, 'LOG');
curl -v -X PUT "http://localhost:1080/mockserver/clear?type=log"

See REST API for full JSON specification

new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .clear({
    'path': '/some/path'
  })
  .then(
    function () {
      console.log("cleared state that matches request matcher");
    },
    function (error) {
      console.log(error);
    }
  );

See REST API for full JSON specification

from mockserver.client import MockServerClient
from mockserver.models import HttpRequest

client = MockServerClient("localhost", 1080)
client.clear(
    HttpRequest.request("/some/path")
)
require 'mockserver-client'

client = MockServer::Client.new('localhost', 1080)
client.clear(
    MockServer::HttpRequest.new(path: '/some/path')
)
import mockserver "github.com/mock-server/mockserver-monorepo/mockserver-client-go/v7"

client := mockserver.New("localhost", 1080)
client.Clear(
    mockserver.Request().Path("/some/path"),
    mockserver.ClearAll,
)
using MockServer.Client;
using MockServer.Client.Models;

using var client = new MockServerClient("localhost", 1080);
client.Clear(
    HttpRequest.Request().WithPath("/some/path")
);
use mockserver_client::{ClientBuilder, HttpRequest};

let client = ClientBuilder::new("localhost", 1080).build().unwrap();
client.clear(
    Some(&HttpRequest::new().path("/some/path")),
    None,
).unwrap();
use MockServer\MockServerClient;
use MockServer\HttpRequest;

$client = new MockServerClient('localhost', 1080);
$client->clear(
    HttpRequest::request()
        ->path('/some/path')
);
curl -v -X PUT "http://localhost:1080/mockserver/clear" -d '{
    "path": "/some/path"
}'

See REST API for full JSON specification

new MockServerClient("localhost", 1080).clear(
    openAPI(
        "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "showPetById"
    )
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .clear({
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    })
    .then(
        function () {
            console.log("cleared state that matches request matcher");
        },
        function (error) {
            console.log(error);
        }
    );

See REST API for full JSON specification

The typed clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

import json
import urllib.request

body = json.dumps({
    "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
    "operationId": "showPetById"
})
req = urllib.request.Request(
    "http://localhost:1080/mockserver/clear",
    data=body.encode("utf-8"),
    method="PUT",
    headers={"Content-Type": "application/json"},
)
urllib.request.urlopen(req)

The typed clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

require 'net/http'
require 'json'

uri = URI('http://localhost:1080/mockserver/clear')
body = JSON.generate({
    'specUrlOrPayload' => 'https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json',
    'operationId' => 'showPetById'
})
request = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json')
request.body = body
Net::HTTP.start(uri.host, uri.port) { |http| http.request(request) }

The typed Clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

import (
    "net/http"
    "strings"
)

body := `{
    "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
    "operationId": "showPetById"
}`
req, _ := http.NewRequest("PUT",
    "http://localhost:1080/mockserver/clear",
    strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
http.DefaultClient.Do(req)

The typed Clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

using System.Net.Http;
using System.Text;

using var httpClient = new HttpClient();
var json = @"{
    ""specUrlOrPayload"": ""https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json"",
    ""operationId"": ""showPetById""
}";
await httpClient.PutAsync(
    "http://localhost:1080/mockserver/clear",
    new StringContent(json, Encoding.UTF8, "application/json")
);

The typed clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

use reqwest::blocking::Client;

let client = Client::new();
let body = r#"{
    "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
    "operationId": "showPetById"
}"#;
client.put("http://localhost:1080/mockserver/clear")
    .header("Content-Type", "application/json")
    .body(body.to_string())
    .send()
    .unwrap();

The typed clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

$json = <<<'JSON'
{
    "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
    "operationId": "showPetById"
}
JSON;

$ch = curl_init('http://localhost:1080/mockserver/clear');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST  => 'PUT',
    CURLOPT_POSTFIELDS     => $json,
    CURLOPT_HTTPHEADER     => ['Content-Type: application/json; charset=utf-8'],
    CURLOPT_RETURNTRANSFER => true,
]);
curl_exec($ch);
curl_close($ch);
curl -v -X PUT "http://localhost:1080/mockserver/clear" -d '{
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    }'

See REST API for full JSON specification

new MockServerClient("localhost", 1080)
    .clear("31e4ca35-66c6-4645-afeb-6e66c4ca0559");
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .clearById("31e4ca35-66c6-4645-afeb-6e66c4ca0559")
    .then(
        function () {
            console.log("cleared state that matches expectation id");
        },
        function (error) {
            console.log(error);
        }
    );

See REST API for full JSON specification

from mockserver.client import MockServerClient

client = MockServerClient("localhost", 1080)
client.clear_by_id("31e4ca35-66c6-4645-afeb-6e66c4ca0559")
require 'mockserver-client'

client = MockServer::Client.new('localhost', 1080)
client.clear_by_id('31e4ca35-66c6-4645-afeb-6e66c4ca0559')
import mockserver "github.com/mock-server/mockserver-monorepo/mockserver-client-go/v7"

client := mockserver.New("localhost", 1080)
client.ClearByID("31e4ca35-66c6-4645-afeb-6e66c4ca0559", mockserver.ClearAll)
using MockServer.Client;

using var client = new MockServerClient("localhost", 1080);
client.ClearById("31e4ca35-66c6-4645-afeb-6e66c4ca0559");
use mockserver_client::ClientBuilder;

let client = ClientBuilder::new("localhost", 1080).build().unwrap();
client.clear_by_id("31e4ca35-66c6-4645-afeb-6e66c4ca0559", None).unwrap();
use MockServer\MockServerClient;

$client = new MockServerClient('localhost', 1080);
$client->clearById('31e4ca35-66c6-4645-afeb-6e66c4ca0559');
curl -X PUT "http://localhost:1080/mockserver/clear" -d '{
    "id": "31e4ca35-66c6-4645-afeb-6e66c4ca0559"
}'

See REST API for full JSON specification

new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path"),
    ClearType.LOG
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .clear({
    'path': '/some/path'
  }, 'LOG')
  .then(
    function () {
      console.log("cleared recorded requests and logs that matches request matcher");
    },
    function (error) {
      console.log(error);
    }
  );

See REST API for full JSON specification

from mockserver.client import MockServerClient
from mockserver.models import HttpRequest

client = MockServerClient("localhost", 1080)
client.clear(
    HttpRequest.request("/some/path"),
    clear_type="LOG"
)
require 'mockserver-client'

client = MockServer::Client.new('localhost', 1080)
client.clear(
    MockServer::HttpRequest.new(path: '/some/path'),
    type: 'LOG'
)
import mockserver "github.com/mock-server/mockserver-monorepo/mockserver-client-go/v7"

client := mockserver.New("localhost", 1080)
client.Clear(
    mockserver.Request().Path("/some/path"),
    mockserver.ClearLog,
)
using MockServer.Client;
using MockServer.Client.Models;

using var client = new MockServerClient("localhost", 1080);
client.Clear(
    HttpRequest.Request().WithPath("/some/path"),
    "LOG"
);
use mockserver_client::{ClientBuilder, HttpRequest, ClearType};

let client = ClientBuilder::new("localhost", 1080).build().unwrap();
client.clear(
    Some(&HttpRequest::new().path("/some/path")),
    Some(ClearType::Log),
).unwrap();
use MockServer\MockServerClient;
use MockServer\HttpRequest;

$client = new MockServerClient('localhost', 1080);
$client->clear(
    HttpRequest::request()
        ->path('/some/path'),
    'LOG'
);
curl -v -X PUT "http://localhost:1080/mockserver/clear?type=log" -d '{
    "path": "/some/path"
}'

See REST API for full JSON specification

new MockServerClient("localhost", 1080).clear(
    openAPI(
        "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "showPetById"
    ),
    ClearType.LOG
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .clear({
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    }, 'LOG')
    .then(
        function () {
            console.log("cleared state that matches request matcher");
        },
        function (error) {
            console.log(error);
        }
    );

See REST API for full JSON specification

The typed clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

import json
import urllib.request

body = json.dumps({
    "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
    "operationId": "showPetById"
})
req = urllib.request.Request(
    "http://localhost:1080/mockserver/clear?type=log",
    data=body.encode("utf-8"),
    method="PUT",
    headers={"Content-Type": "application/json"},
)
urllib.request.urlopen(req)

The typed clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

require 'net/http'
require 'json'

uri = URI('http://localhost:1080/mockserver/clear?type=log')
body = JSON.generate({
    'specUrlOrPayload' => 'https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json',
    'operationId' => 'showPetById'
})
request = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json')
request.body = body
Net::HTTP.start(uri.host, uri.port) { |http| http.request(request) }

The typed Clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

import (
    "net/http"
    "strings"
)

body := `{
    "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
    "operationId": "showPetById"
}`
req, _ := http.NewRequest("PUT",
    "http://localhost:1080/mockserver/clear?type=log",
    strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
http.DefaultClient.Do(req)

The typed Clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

using System.Net.Http;
using System.Text;

using var httpClient = new HttpClient();
var json = @"{
    ""specUrlOrPayload"": ""https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json"",
    ""operationId"": ""showPetById""
}";
await httpClient.PutAsync(
    "http://localhost:1080/mockserver/clear?type=log",
    new StringContent(json, Encoding.UTF8, "application/json")
);

The typed clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

use reqwest::blocking::Client;

let client = Client::new();
let body = r#"{
    "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
    "operationId": "showPetById"
}"#;
client.put("http://localhost:1080/mockserver/clear?type=log")
    .header("Content-Type", "application/json")
    .body(body.to_string())
    .send()
    .unwrap();

The typed clear method takes a request matcher; to clear by an OpenAPI matcher send the JSON body to the control-plane endpoint directly.

$json = <<<'JSON'
{
    "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
    "operationId": "showPetById"
}
JSON;

$ch = curl_init('http://localhost:1080/mockserver/clear?type=log');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST  => 'PUT',
    CURLOPT_POSTFIELDS     => $json,
    CURLOPT_HTTPHEADER     => ['Content-Type: application/json; charset=utf-8'],
    CURLOPT_RETURNTRANSFER => true,
]);
curl_exec($ch);
curl_close($ch);
curl -v -X PUT "http://localhost:1080/mockserver/clear?type=log" -d '{
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver-monorepo/master/mockserver/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    }'

See REST API for full JSON specification

new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path"),
    ClearType.EXPECTATIONS
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .clear({
    'path': '/some/path'
  }, 'EXPECTATIONS')
  .then(
    function () {
      console.log("cleared expectations that matches request matcher");
    },
    function (error) {
      console.log(error);
    }
  );

See REST API for full JSON specification

from mockserver.client import MockServerClient
from mockserver.models import HttpRequest

client = MockServerClient("localhost", 1080)
client.clear(
    HttpRequest.request("/some/path"),
    clear_type="EXPECTATIONS"
)
require 'mockserver-client'

client = MockServer::Client.new('localhost', 1080)
client.clear(
    MockServer::HttpRequest.new(path: '/some/path'),
    type: 'EXPECTATIONS'
)
import mockserver "github.com/mock-server/mockserver-monorepo/mockserver-client-go/v7"

client := mockserver.New("localhost", 1080)
client.Clear(
    mockserver.Request().Path("/some/path"),
    mockserver.ClearExpectations,
)
using MockServer.Client;
using MockServer.Client.Models;

using var client = new MockServerClient("localhost", 1080);
client.Clear(
    HttpRequest.Request().WithPath("/some/path"),
    "EXPECTATIONS"
);
use mockserver_client::{ClientBuilder, HttpRequest, ClearType};

let client = ClientBuilder::new("localhost", 1080).build().unwrap();
client.clear(
    Some(&HttpRequest::new().path("/some/path")),
    Some(ClearType::Expectations),
).unwrap();
use MockServer\MockServerClient;
use MockServer\HttpRequest;

$client = new MockServerClient('localhost', 1080);
$client->clear(
    HttpRequest::request()
        ->path('/some/path'),
    'EXPECTATIONS'
);
curl -v -X PUT "http://localhost:1080/mockserver/clear?type=expectations" -d '{
    "path": "/some/path"
}'

See REST API for full JSON specification

MockServer can be reset completely, as follows:

new MockServerClient("localhost", 1080).reset();
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .reset()
  .then(
    function () {
      console.log("reset all state");
    },
    function (error) {
      console.log(error);
    }
  );

See REST API for full JSON specification

from mockserver.client import MockServerClient

client = MockServerClient("localhost", 1080)
client.reset()
require 'mockserver-client'

client = MockServer::Client.new('localhost', 1080)
client.reset
import mockserver "github.com/mock-server/mockserver-monorepo/mockserver-client-go/v7"

client := mockserver.New("localhost", 1080)
client.Reset()
using MockServer.Client;

using var client = new MockServerClient("localhost", 1080);
client.Reset();
use mockserver_client::ClientBuilder;

let client = ClientBuilder::new("localhost", 1080).build().unwrap();
client.reset().unwrap();
use MockServer\MockServerClient;

$client = new MockServerClient('localhost', 1080);
$client->reset();
curl -v -X PUT "http://localhost:1080/mockserver/reset"

See REST API for full JSON specification