---
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: 'General'
subsection: true
sitemap:
  priority: 0.7
  changefreq: 'monthly'
  lastmod: 2019-11-10T08:00:00+01:00
---

<p>MockServer has the following internal state:</p>

<ul>
    <li><strong>recorded requests</strong></li>
    <li><strong>active expectations</strong></li>
    <li><strong>recorded expectations</strong></li>
    <li><strong>logs</strong></li>
</ul>

<p>State can be <strong>cleared</strong> from MockServer selectively:</p>
<ul>
    <li>use <strong>type</strong> to select which type of state to clear, supported values are: <span class="string_literal">all</span>, <span class="string_literal">log</span>, <span class="string_literal">expectations</span></li>
    <li>use a <a href="/mock_server/getting_started.html#request_matchers"><strong>request matcher</strong></a> to clear matching items</li>
    <li>use an <strong>expectation id</strong> to clear matching items</li>
</ul>

<p><strong>How clearing works:</strong> when <a href="/mock_server/debugging_issues.html#logging-levels">logLevel</a> is <span class="string_literal">INFO</span> or lower (the default), cleared log entries are <em>soft-deleted</em> rather than physically removed. This means:</p>
<ul>
    <li><strong>Verification works correctly</strong> &mdash; the <a href="/mock_server/verification.html">verify</a> endpoint ignores cleared entries, so verifying a request after clearing will behave as if the logs were fully removed</li>
    <li><strong>Retrieve works correctly</strong> &mdash; the retrieve endpoint for requests, recorded expectations, and log messages filters out cleared entries</li>
    <li><strong>UI may still show cleared entries</strong> &mdash; the <a href="/mock_server/mockserver_ui.html">UI</a> can display soft-deleted entries (marked as deleted) to help debug issues</li>
</ul>
<p>If you want cleared log entries to be physically removed from memory, set the log level to <span class="string_literal">WARN</span> or higher.</p>

<button id="button_client_clear_all_logs" class="accordion">clear all recorded requests and logs</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    request(),
    ClearType.LOG
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">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);
    }
  );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear?type=log"</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_clear" class="accordion">clear everything that matches request properties matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path")
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">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);
    }
  );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear" -d '{
    "path": "/some/path"
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_openapi_clear" class="accordion">clear everything that matches openapi request matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">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"
    )
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">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);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">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"
    }'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_expectation_id_clear" class="accordion">clear everything that matches expectation id</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .clear("31e4ca35-66c6-4645-afeb-6e66c4ca0559");</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">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);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -X PUT "http://localhost:1080/mockserver/clear" -d '{
    "id": "31e4ca35-66c6-4645-afeb-6e66c4ca0559"
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_clear_logs_by_req" class="accordion">clear recorded requests and logs that matches request properties matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path"),
    ClearType.LOG
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">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);
    }
  );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear?type=log" -d '{
    "path": "/some/path"
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_clear_logs_by_openapi_req" class="accordion">clear recorded requests and logs that matches openapi request matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">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
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">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);
        }
    );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">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"
    }'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_clear_expectations_by_req" class="accordion">clear expectations that matches request matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path"),
    ClearType.EXPECTATIONS
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">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);
    }
  );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear?type=expectations" -d '{
    "path": "/some/path"
}'</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>

<p>MockServer can be <strong>reset</strong> completely, as follows:</p>

<button id="button_client_reset" class="accordion">reset everything</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
        <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).reset();</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .reset()
  .then(
    function () {
      console.log("reset all state");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_reset" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/reset"</code></pre>
        <p>See <a  href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_reset" target="_blank" rel="noopener noreferrer">REST API</a> for full JSON specification</p>
    </div>
</div>
