Skip to main content

Delete Document

Deletes one or more documents from a MongoDB collection that match a specified query filter.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

warning

This operation permanently deletes documents. Deleted documents cannot be recovered.

Inputs

  • MongoDB Client Id - The client ID returned from the Connect node (optional if credentials are provided).
  • Database Name - The name of the database containing the collection.
  • Collection Name - The name of the collection to delete documents from.
  • MongoDB Query - MongoDB query filter in JSON format to match documents for deletion. Documents matching this filter will be deleted.

Options

  • Credentials - Database credentials (Category 5) - optional if using Client ID from Connect node. This allows you to perform the operation without a separate Connect node.

Output

  • Result - The number of documents that were successfully deleted.

How It Works

The Delete Document node removes documents from a MongoDB collection based on a query filter. When executed, the node:

  1. Validates that database name and collection name are not empty
  2. Obtains a MongoDB client (either from client ID or by creating one from credentials)
  3. Processes the MongoDB Query input with Handlebars template rendering
  4. Parses the rendered JSON into BSON format
  5. Accesses the specified collection
  6. Executes DeleteMany operation with the provided filter
  7. Returns the count of deleted documents

Requirements

  • Either a valid client ID from Connect node OR database credentials
  • Valid database name (non-empty)
  • Valid collection name (non-empty)
  • Valid JSON query filter
  • Appropriate permissions to delete documents in the collection

Error Handling

The node will return specific errors in the following cases:

  • ErrInvalidArg - Database name or collection name is empty, or client/credentials are invalid
  • ErrConnection - Cannot connect to MongoDB (when using credentials)
  • JSON parsing errors if the query filter format is invalid
  • Permission errors if the user doesn't have delete rights
  • Collection not found errors if the collection doesn't exist

Usage Notes

  • Uses DeleteMany operation, which deletes all documents matching the filter
  • Returns the number of documents deleted, which can be zero if no matches found
  • An empty filter {} will delete ALL documents in the collection
  • The MongoDB Query field supports Handlebars templates for dynamic values
  • The operation is atomic for each document
  • You can use either the client ID approach or direct credentials approach
  • Deleted documents cannot be recovered unless you have backups

Query Filter Format

The query filter uses MongoDB query syntax in JSON format:

Delete by exact match:

{
"status": "inactive"
}

Delete by multiple conditions:

{
"status": "pending",
"created_date": {
"$lt": "2023-01-01"
}
}

Delete by ID:

{
"_id": {
"$oid": "507f1f77bcf86cd799439011"
}
}

Delete all documents (use with caution):

{}

Example Usage

Scenario 1: Delete inactive users

  1. Connect node → Client Id
  2. Delete Document:
    • MongoDB Client Id: (from Connect)
    • Database Name: "myapp"
    • Collection Name: "users"
    • MongoDB Query:
      {
      "status": "inactive"
      }
    • Output: deleted_count
  3. Log: "Deleted " + deleted_count + " inactive users"

Scenario 2: Delete old records using direct credentials

  1. Delete Document:
    • Database Name: "logs"
    • Collection Name: "audit_logs"
    • Credentials: (select credential)
    • MongoDB Query:
      {
      "timestamp": {
      "$lt": "2023-01-01"
      }
      }
    • Output: result

Scenario 3: Delete with dynamic values using Handlebars

Set Variable:
- target_status = "cancelled"

Delete Document:
- Database Name: "orders"
- Collection Name: "sales"
- MongoDB Query:
{
"status": "{{target_status}}"
}
- Output: deleted_count

Log: "Deleted {{deleted_count}} cancelled orders"

Scenario 4: Delete documents matching multiple criteria

Delete Document:
- Database Name: "inventory"
- Collection Name: "products"
- MongoDB Query:
{
"stock": 0,
"discontinued": true,
"last_updated": {
"$lt": "2022-01-01"
}
}
- Output: removed_count

If removed_count > 0:
Log: "Removed " + removed_count + " obsolete products"

Scenario 5: Safe deletion with confirmation

Set Variable:
- collection_name = "temp_data"

If collection_name contains "temp" or "test":
Delete Document:
- Database Name: "testing"
- Collection Name: {collection_name}
- MongoDB Query: {}
- Output: deleted_all
Log: "Cleared " + deleted_all + " test documents"
Else:
Log: "Cannot delete from production collection"

Scenario 6: Delete by array of IDs

Set Variable:
- ids_to_delete = ["507f1f77bcf86cd799439011", "507f1f77bcf86cd799439012"]

Delete Document:
- Database Name: "archive"
- Collection Name: "old_records"
- MongoDB Query:
{
"_id": {
"$in": [
{"$oid": "507f1f77bcf86cd799439011"},
{"$oid": "507f1f77bcf86cd799439012"}
]
}
}

Common Use Cases

  • Data Cleanup: Remove old, obsolete, or temporary records
  • User Management: Delete inactive or banned user accounts
  • Testing: Clear test data after automated tests
  • GDPR Compliance: Remove user data upon request
  • Archival: Delete records after archiving to external storage
  • Maintenance: Remove duplicate or corrupted documents
  • State Management: Delete expired sessions or tokens

MongoDB Query Operators

Common operators you can use in the query filter:

  • $eq - Equal to
  • $ne - Not equal to
  • $gt - Greater than
  • $gte - Greater than or equal to
  • $lt - Less than
  • $lte - Less than or equal to
  • $in - Matches any value in an array
  • $nin - Matches none of the values in an array
  • $and - Logical AND
  • $or - Logical OR
  • $not - Logical NOT
  • $exists - Field exists
  • $regex - Regular expression match

Best Practices

  • Always test your query filter with Read Document first to verify matches
  • Use specific filters to avoid accidental deletion of wrong documents
  • Log the deleted count to track deletions
  • Consider archiving important data before deletion
  • Use transactions if deletion must be coordinated with other operations
  • Implement soft deletes (status flags) instead of hard deletes when possible
  • Backup data before performing bulk deletions
  • Use appropriate indexes for better delete performance

Safety Tips

  • Never use an empty filter {} in production without explicit confirmation
  • Test deletion queries in development environment first
  • Consider implementing a confirmation step before deletion
  • Use read operations to preview what will be deleted
  • Implement role-based access control to restrict delete operations
  • Monitor deletion operations for suspicious patterns

Common Errors

Empty Database Name:

  • Cause: Database name input is empty
  • Solution: Provide a valid database name

Empty Collection Name:

  • Cause: Collection name input is empty
  • Solution: Provide a valid collection name

Invalid Query JSON:

  • Cause: MongoDB Query field contains malformed JSON
  • Solution: Validate your JSON syntax

Permission Denied:

  • Cause: User doesn't have delete permission
  • Solution: Ensure user has readWrite or dbAdmin role

Collection Not Found:

  • Cause: Specified collection doesn't exist
  • Solution: Verify collection name or create the collection first
  • Insert Document - Add new documents
  • Update Document - Modify existing documents
  • Read Document - Query documents (useful for previewing delete targets)
  • Drop Collection - Delete entire collection including all documents