<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="mysql-shell-tutorial-python-documents-remove">
      </a>
      22.4.3.5 Remove Documents
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045127025552">
  </a>
  <p>
   You can use the
   <code class="literal">
    remove()
   </code>
   method to delete
        some or all documents from a collection in a schema. The
        X DevAPI provides additional methods for use with the
   <code class="literal">
    remove()
   </code>
   method to filter and sort the
        documents to be removed.
  </p>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-python-remove-documents-by-condition">
       </a>
       Remove Documents Using Conditions
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127020768">
   </a>
   <p>
    The following example passes a search condition to the
    <code class="literal">
     remove()
    </code>
    method. All documents matching the
          condition are removed from the
    <code class="literal">
     countryinfo
    </code>
    collection. In this example, one document matches the
          condition.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-mysqlsh"><div class="docs-select-all right" id="sa73207124"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-py&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">"Code = 'SEA'"</span><span class="token punctuation">)</span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-python-remove-first-document">
       </a>
       Remove the First Document
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127014144">
   </a>
   <p>
    To remove the first document in the
    <code class="literal">
     countryinfo
    </code>
    collection, use the
    <code class="literal">
     limit()
    </code>
    method with a value of 1.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-mysqlsh"><div class="docs-select-all right" id="sa24981317"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-py&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">"true"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-python-remove-last-document">
       </a>
       Remove the Last Document in an Order
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127007824">
   </a>
   <p>
    The following example removes the last document in the
    <code class="literal">
     countryinfo
    </code>
    collection by country name.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-mysqlsh"><div class="docs-select-all right" id="sa9625025"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-mysqlsh"><span class="token prompt">mysql-py&gt;</span> db<span class="token punctuation">.</span>countryinfo<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">"true"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">"Name desc"</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-python-remove-all-documents">
       </a>
       Remove All Documents in a Collection
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127002112">
   </a>
   <p>
    You can remove all documents in a collection. To do so, use
          the
    <code class="literal">
     remove("true")
    </code>
    method without
          specifying a search condition.
   </p>
   <div class="caution" style="margin-left: 0.5in; margin-right: 0.5in;">
    <div class="admon-title">
     Caution
    </div>
    <p>
     Use care when you remove documents without specifying a
            search condition. This action deletes all documents from the
            collection.
    </p>
   </div>
   <p>
    Alternatively, use the
    <code class="literal">
     db.drop_collection('countryinfo')
    </code>
    operation
          to delete the
    <code class="literal">
     countryinfo
    </code>
    collection.
   </p>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="idm46045126996416">
       </a>
       Related Information
      </h5>
     </div>
    </div>
   </div>
   <div class="itemizedlist">
    <ul class="itemizedlist" style="list-style-type: disc; ">
     <li class="listitem">
      <p>
       See
       <a class="ulink" href="/doc/x-devapi-userguide/en/crud-ebnf-collection-crud-functions.html#crud-ebnf-collectionremovefunction" target="_top">
        CollectionRemoveFunction
       </a>
       for the full syntax definition.
      </p>
     </li>
     <li class="listitem">
      <p>
       See
       <a class="xref" href="mysql-shell-tutorial-python-download.html" title="22.4.2 Download and Import world_x Database">
        Section 22.4.2, “Download and Import world_x Database”
       </a>
       for instructions to recreate the
       <code class="literal">
        world_x
       </code>
       schema.
      </p>
     </li>
    </ul>
   </div>
  </div>
 </div>
 <br/>
</div>
