Delete

Delete one or several documents that match a filter.

Delete documents with deleteOne or deleteMany. To delete documents by age without scheduling it, use a TTL index.

Requirements

  • A connection with a user that has readWrite on the database.

Delete a document

JavaScript
db.orders.deleteOne({ _id: ObjectId("65f1c0a2e4b0a1b2c3d4e5f6") });

Delete several

JavaScript
db.orders.deleteMany({ status: "cancelled" });

Check

The response carries deletedCount with the number of deleted documents, which in this example are all the orders with status: "cancelled" that the collection held.

Common problems

  • deleteMany({}) deletes every document but not the collection or its indexes; drop is for that.

See also