<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>delete all documents in collection mongodb compass - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/tag/delete-all-documents-in-collection-mongodb-compass/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Fri, 06 Jan 2023 00:38:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>delete all documents in collection mongodb compass - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MongoDB query Remove/Delete records in a Collection</title>
		<link>https://thecodebuzz.com/mongodb-delete-records-multiple-compass-mongo-shell-from-collection-mongoose/</link>
					<comments>https://thecodebuzz.com/mongodb-delete-records-multiple-compass-mongo-shell-from-collection-mongoose/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 30 Apr 2022 03:40:00 +0000</pubDate>
				<category><![CDATA[MongoDB and Mongoose]]></category>
		<category><![CDATA[delete all documents in collection mongodb compass]]></category>
		<category><![CDATA[how to delete a collection in mongodb]]></category>
		<category><![CDATA[mongodb delete all documents]]></category>
		<category><![CDATA[mongodb delete one]]></category>
		<category><![CDATA[mongodb find and delete]]></category>
		<category><![CDATA[mongodb remove document by _id]]></category>
		<category><![CDATA[mongodb remove field]]></category>
		<category><![CDATA[mongodb remove with multiple conditions]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22266</guid>

					<description><![CDATA[<p>MongoDB query Remove/Delete records in a Collection Today in this article, we will cover how to write MongoDB query Remove/Delete records in a Collection. We will see a simple query to delete all the records and also delete records based on filter criteria. We will use the Mongo Shell query to perform the deletes. If [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/mongodb-delete-records-multiple-compass-mongo-shell-from-collection-mongoose/">MongoDB query Remove/Delete records in a Collection</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>MongoDB query Remove/Delete records in a Collection</strong></h1>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="292" src="https://www.thecodebuzz.com/wp-content/uploads/2022/05/how-to-delete-records-completely-from-a-mongodb-collection-1024x292.jpg" alt="MongoDB query Delete records" class="wp-image-22271" srcset="https://thecodebuzz.com/wp-content/uploads/2022/05/how-to-delete-records-completely-from-a-mongodb-collection-1024x292.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/05/how-to-delete-records-completely-from-a-mongodb-collection-300x86.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/05/how-to-delete-records-completely-from-a-mongodb-collection-768x219.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/05/how-to-delete-records-completely-from-a-mongodb-collection-1536x438.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/05/how-to-delete-records-completely-from-a-mongodb-collection-785x224.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/05/how-to-delete-records-completely-from-a-mongodb-collection.jpg 1633w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Today in this article, we will cover how to write MongoDB query Remove/Delete records in a Collection. We will see a simple query to delete all the records and also delete records based on filter criteria.</p>



<p></p>



<p>We will use the Mongo Shell query to perform the deletes. If you are interested to perform delete within Atlas UI compass please refer to this article.</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-mongodb-delete-all-records">MongoDB Delete All records</a></li><li><a href="#aioseo-switch-to-the-database">Switch to the Database</a></li><li><a href="#aioseo-delete-all-records-using-deletemany">Delete all records using deleteMany</a></li><li><a href="#aioseo-delete-records-for-filter-criteria-using-deletemany-methods">Delete records for filter criteria using deleteMany methods</a></li><li><a href="#aioseo-delete-all-records-using-remove-methods">Delete all records using remove() methods</a></li><li><a href="#aioseo-delete-records-for-filter-criteria-using-remove-methods">Delete records for filter criteria using remove() methods</a></li><li><a href="#aioseo-delete-records-using-the-unset-operator">Delete records using the Unset operator</a></li></ul></div>



<p></p>



<p>You may find the need to delete the records from a mongo collection from time to time, especially when performing the schema update or data cleanup.</p>



<p></p>



<p>While dropping the collection is another easier way to delete the records which not only deletes the collection but also deletes all the records along with indexes if any.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-mongodb-delete-all-records">MongoDB Delete All records </h2>



<p></p>



<p>But if you want to delete the records without deleting the collection or indexes then you can use the below command from the mongo shell</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-switch-to-the-database">Switch to the Database </h2>



<p></p>



<p>Switch to the Database using the command &#8220;use&#8221;</p>



<p></p>



<p><strong>Command </strong>:</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">use &lt;Your DB&gt;</pre>



<p> </p>



<p><strong>Example </strong></p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-1024x136.jpg" alt="MongoDB Delete all records, MongoDB query Remove/Delete records in a Collection" class="wp-image-22268" width="530" height="70" srcset="https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-1024x136.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-300x40.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-768x102.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-785x104.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records.jpg 1114w" sizes="(max-width: 530px) 100vw, 530px" /></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-delete-all-records-using-deletemany">Delete all records using deleteMany</h2>



<p></p>



<p>We can easily delete all records using <strong><em>deleteMany</em>()</strong> method </p>



<p></p>



<p><strong>Command </strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">db.&lt;collection name&gt;.deleteMany({})</pre>



<p></p>



<p><strong>Example </strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"> db.employee1.deleteMany({}) </pre>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="251" src="https://www.thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-using-DeleteMany-Mongoshell-atlas-mongooose-1024x251.jpg" alt="MongoDB query Delete records" class="wp-image-22270" srcset="https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-using-DeleteMany-Mongoshell-atlas-mongooose-1024x251.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-using-DeleteMany-Mongoshell-atlas-mongooose-300x74.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-using-DeleteMany-Mongoshell-atlas-mongooose-768x188.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-using-DeleteMany-Mongoshell-atlas-mongooose-1536x376.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-using-DeleteMany-Mongoshell-atlas-mongooose-785x192.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/05/MongoDB-Delete-All-records-using-DeleteMany-Mongoshell-atlas-mongooose.jpg 1714w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-delete-records-for-filter-criteria-using-deletemany-methods">Delete records for filter criteria using deleteMany methods</h2>



<p></p>



<p><strong>Example </strong>1:</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">db.employee1.deleteOne({Name:"Meery"})
</pre>



<p>Above query will delete the first matching records(Where Name fields match the value as &#8220;Merry&#8221; ) for the given filter condition</p>



<p></p>



<p></p>



<p><strong>Example </strong>2:</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">db.employee1.deleteMany({Name:"Meery"})
</pre>



<p>The above query will delete all matching records(one or multiple) for the given filter condition.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-delete-all-records-using-remove-methods">Delete all records using <em>remove() </em>methods </h2>



<p></p>



<p></p>



<p>We can also easily delete all records using <strong><em>remove</em>()</strong> method.</p>



<p></p>



<p><strong>Command </strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">db.&lt;collection name&gt;.remove({})</pre>



<p></p>



<p><strong>Example </strong>1</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">db.employee1.remove({}) </pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-delete-records-for-filter-criteria-using-remove-methods">Delete records for filter criteria using <em>remove</em>() methods  </h2>



<p></p>



<p><strong>Example </strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">db.employee1.remove({Name:"John"})

</pre>



<p id="block-8b715b1d-4c55-49e9-adac-ec9664b7d2d5">Above query will remove records where Name fields match with value of <strong>&#8220;John&#8221;</strong></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-delete-records-using-the-unset-operator">Delete records using the <em>Unset </em>operator </h2>



<p></p>



<p>One can also delete the records using <em><strong>unset </strong></em>operator too.</p>



<p></p>



<p>For more details please visit below,</p>



<p></p>



<ul class="wp-block-list"><li><em><strong> <a href="https://www.thecodebuzz.com/mongodb-remove-fields-delete-fields-document-unset-command-shell-cli/" target="_blank" rel="noreferrer noopener" title="MongoDB – How to Remove fields from document">MongoDB – Using Unset$ operator to Remove fields from Mongo Document</a></strong></em></li></ul>



<p></p>



<p></p>



<p><strong>References : </strong></p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/mongodb-remove-fields-delete-fields-document-unset-command-shell-cli/" target="_blank" rel="noreferrer noopener" title="MongoDB – How to Remove fields from document"><em><strong>MongoDB – How to Remove fields from document</strong></em></a></li></ul>



<p></p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/mongodb-delete-records-multiple-compass-mongo-shell-from-collection-mongoose/">MongoDB query Remove/Delete records in a Collection</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mongodb-delete-records-multiple-compass-mongo-shell-from-collection-mongoose/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
