Skip to content

Optimize deleting all rows #134

Open

Description

@coffeemug

r.table('tablename').delete.run can really be done in constant time by dropping and recreating the table (well, it isn't quite that simple because we need to maintain all the preferences for that table, but there's no reason for this query to be linear time either). (suggested by Damir)

Activity

  1. wojons commented on Nov 22, 2013

    @wojons
    Contributor

    I have come across this issue also that most cases i just drop the table. is rethinkdb friendly about resusing old disk space?

  2. coffeemug commented on Nov 22, 2013

    @coffeemug
    ContributorAuthor

    If you drop the table Rethink just deletes the file on the underlying file system. There shouldn't be any issues with doing it.

  3. wojons commented on Nov 22, 2013

    @wojons
    Contributor

    Perfect :)

    One think i have noticed when doing a big delete job with many shards (10) is that it deleting from one range at a time not from from all shards at the same time.

  4. karlgrz commented on Aug 9, 2016

    @karlgrz

    I just tried running the following query on a table with approximately 5 million records:

    r.db('my_db_name').table('my_table_name').delete();
    

    It timed out. Am I correct that this is the expected behavior? Or should this be a fast operation?

    Cluster topology: 3 nodes, this table only replicated onto a single node without sharding.

  5. danielmewes commented on Aug 9, 2016

    @danielmewes
    Member

    @karlgrz This is still expected behavior today. You can somewhat speed it up, by doing

    r.db('my_db_name').table('my_table_name').delete({durability: "soft"});
    

    Or you can speed it up a lot by dropping the table and recreating it (though you'll have to re-create your replication/sharding configuration and secondary indexes of course).

  6. karlgrz commented on Aug 9, 2016

    @karlgrz

    @danielmewes that's what I figured. Thanks for the drop / recreate suggestion. That will probably be perfectly fine in this situation. 👍

  7. thomasmodeneis commented on Oct 30, 2016

    @thomasmodeneis

    @danielmewes would be nice to add this to documentation!

  8. cristicbz commented on May 19, 2019

    @cristicbz

    @danielmewes for a small table, dropping and recreating is actually slower. In our integration tests, we create one fake database per test thread, each with the same schema as the real thing.

    Between test cases we delete all the data in all the tables (r.db(db_name).table_list().for_each(lambda table: r.db(db_name).table(table).delete()). This takes ~10ms (we've got about 20 tables) compared to around 20 seconds to drop and recreate all the tables and indexes.

    Since we're doing this in tests, the tables never have too much data in them. We are still bottlenecked by those 10ms (with generative tests, we easily have 500 examples/test). So I'm all for the optimisation, but not if it makes it slower for the small table case!

  9. added a commit that references this issue on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions