How to Configure OpenHands MongoDB Prevent Delete
To set up an openhands mongodb prevent delete rule, assign roles without remove actions. You can use the built-in read role or build a custom role. Teams often give coding agents broad database accounts to move fast during testing. Still, an agent with full write rights can wipe out whole collections by mistake. One bad prompt or loop can delete production records in seconds. How to set up openhands mongodb prevent delete rules MongoDB controls what users and agents can do through role-based
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
To set up an openhands mongodb prevent delete rule, assign roles without remove actions. You can use the built-in read role or build a custom role. Teams often give coding agents broad database accounts to move fast during testing. Still, an agent with full write rights can wipe out whole collections by mistake. One bad prompt or loop can delete production records in seconds.
How to set up openhands mongodb prevent delete rules
MongoDB controls what users and agents can do through role-based access rules. Each role grants a set of privilege actions on specific database resources. A role applies to the database where an admin defines it. Admins can scope these rules down to a single collection level. Notice that MongoDB Atlas and self-hosted setups have different built-in role lists. Still, both systems build their roles from the exact same privilege actions.
The fastest way to block deletes is to assign the built-in read role. The read role lets the agent view non-system collections in that database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It leaves out the insert, update, remove, and dropCollection privilege actions. When an OpenHands agent uses a read role, the database blocks any delete command.
Sometimes an OpenHands agent needs to add or change records during a task. The standard readWrite role gives full read access plus the power to change data. Still, readWrite includes remove, dropCollection, and dropIndex alongside insert and update. If you assign readWrite, the agent can still delete rows and drop collections. To allow writes without deletes, you must create a custom role in MongoDB. Your custom role should grant find, insert, and update on target collections. Leave the remove and dropCollection actions out of that custom role definition. Next, create a dedicated database user for the OpenHands runtime and attach the role. Finally, pass those user credentials to the agent workspace so it can log in.
What native roles solve and what they leave open
Native database roles do a great job of stopping broad action types. If a role lacks the remove action, the database rejects every delete call. This stops an autonomous agent from wiping a collection with a remove command. It also stops the agent from dropping an entire collection during a test run. For pure read-only tasks, the built-in read role is simple and safe.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Yet native roles only check who logs in and which action type runs. They do not inspect what individual statements or updates do in flight. An agent with update permissions can still overwrite every field with blank values. A bad update query without a filter can ruin thousands of records at once. From the database point of view, that destructive update is completely valid. Native roles also cannot require human approval before a risky write command runs.
Also, database roles do not mask sensitive fields in query results. When the agent runs a find action, it receives full documents back. Those documents might hold emails, phone numbers, card numbers, or national IDs. That raw data flows straight into the agent context and model prompts. Managing custom roles across dozens of databases also creates extra work for admins. Teams must update role definitions every time a new collection appears.
Inspect MongoDB wire traffic in flight
Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev is an open-source proxy that sits next to MongoDB as a sidecar for your MongoDB. It reads every command from mongosh, drivers, or agent tools.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines. Next, hoop.dev data masking rewrites sensitive values in returned documents based on your rules and logs each decision. Explore the repo on GitHub.
Frequently asked questions
Does the MongoDB readWrite role stop an OpenHands agent from deleting data?
No, the built-in readWrite role includes the remove and dropCollection privilege actions. An agent using readWrite can delete single documents or drop entire collections.
How can an OpenHands agent insert data without delete permissions in MongoDB?
You can build a custom role that grants find, insert, and update actions. Leave out the remove and dropCollection actions so the database rejects delete calls.
Can MongoDB roles stop an agent from reading sensitive fields in a collection?
MongoDB roles work down to the collection level, not the individual field level. If a role grants find on a collection, the query returns all fields.
Related guides
In this post