diff --git a/bin/reth/src/db/clear.rs b/bin/reth/src/db/clear.rs new file mode 100644 index 0000000000..0bd816db8b --- /dev/null +++ b/bin/reth/src/db/clear.rs @@ -0,0 +1,41 @@ +use clap::Parser; + +use reth_db::{ + database::Database, + table::Table, + transaction::{DbTx, DbTxMut}, + TableViewer, Tables, +}; + +/// The arguments for the `reth db clear` command +#[derive(Parser, Debug)] +pub struct Command { + /// Table name + #[arg()] + pub table: Tables, +} + +impl Command { + /// Execute `db clear` command + pub fn execute(self, db: &DB) -> eyre::Result<()> { + self.table.view(&ClearViewer { db })?; + + Ok(()) + } +} + +struct ClearViewer<'a, DB: Database> { + db: &'a DB, +} + +impl TableViewer<()> for ClearViewer<'_, DB> { + type Error = eyre::Report; + + fn view(&self) -> Result<(), Self::Error> { + let tx = self.db.tx_mut()?; + tx.clear::()?; + tx.commit()?; + + Ok(()) + } +} diff --git a/bin/reth/src/db/mod.rs b/bin/reth/src/db/mod.rs index 4670683c43..04b51a1546 100644 --- a/bin/reth/src/db/mod.rs +++ b/bin/reth/src/db/mod.rs @@ -17,6 +17,7 @@ use reth_db::{ use reth_primitives::ChainSpec; use std::sync::Arc; +mod clear; mod get; mod list; /// DB List TUI @@ -71,6 +72,8 @@ pub enum Subcommands { Get(get::Command), /// Deletes all database entries Drop, + /// Deletes all table entries + Clear(clear::Command), /// Lists current and local database versions Version, /// Returns the full database path @@ -172,6 +175,10 @@ impl Command { let mut tool = DbTool::new(&db, self.chain.clone())?; tool.drop(db_path)?; } + Subcommands::Clear(command) => { + let db = open_db(&db_path, self.db.log_level)?; + command.execute(&db)?; + } Subcommands::Version => { let local_db_version = match get_db_version(&db_path) { Ok(version) => Some(version),