Search code examples
pythonpython-3.xfirebasefirebase-realtime-databasepyrebase

"error" : "Data to write exceeds the maximum size that can be modified with a single request." Firebase


I'm trying to delete a node composed of many children in Firebase realtime-database.

I created this little function which is working but not when there are too many children :

def delete_node_firebase(node):
    node_val = db.child(node).get().val()
    for k, v in node_val.items():
        db.child(node).child(k).remove()

When I'm doing a call on a node with a lot of children (in my case more than 20000), this error appears :

"error" : "Data to write exceeds the maximum size that can be modified with a single request.

Does someone know a solution for this please ?


Solution

  • You're trying to write/delete too much data in a single request, which would take more time than the database thinks is reasonable. Instead of allowing this operation and potentially blocking access to your database for other calls, the common approach is to delete the child nodes in more reasonable batches. This has been covered quite a few times, so I recommend checking some of these search results.

    As Doug commented, this blog post How to Perform Large Deletes in the Realtime Database explains how/when the Firebase command line tools implements such a chunked delete. If you want to implement it in your own code, you can have a look at the code of the CLI command for inspiration.

    The CLI and REST API also allow you to pass a parameter to indicate you don't want it to check for large deletes/writes, so have a look at the documentation on writeSizeLimit for that. This parameter is not wrapped in Pyrebase as far as I can tell, so you'll have to call the REST API yourself if you want to use it.

    Also see: