Skip to main content

snarkos_cli/commands/
clean.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::helpers::args::parse_node_data_dir;
17
18use snarkos_utilities::NodeDataDir;
19
20use snarkvm::console::network::{CanaryV0, MainnetV0, Network};
21
22use aleo_std::StorageMode;
23use anyhow::{Context, Result};
24use clap::Parser;
25use colored::Colorize;
26use std::path::PathBuf;
27
28/// Cleans the snarkOS node storage.
29#[derive(Debug, Parser)]
30pub struct Clean {
31    /// Specify the network to remove from storage (0 = mainnet, 1 = testnet, 2 = canary)
32    #[clap(default_value_t=MainnetV0::ID, long = "network", value_parser = clap::value_parser!(u16).range((MainnetV0::ID as i64)..=(CanaryV0::ID as i64)))]
33    pub network: u16,
34
35    /// Enables development mode, specify the unique ID of the local node to clean.
36    #[clap(long)]
37    pub dev: Option<u16>,
38
39    /// Specify the path to a directory containing the ledger. Overrides the default path (also for dev).
40    #[clap(long, alias = "path")]
41    pub ledger_storage: Option<PathBuf>,
42
43    /// Keep the node data directory (disabled by default).
44    #[clap(long)]
45    pub keep_node_data: bool,
46
47    /// Sets a custom path for the node configuration. Overrides the default path (also for dev).
48    #[clap(long, alias = "node-data-path", conflicts_with = "keep_node_data")]
49    pub node_data_storage: Option<PathBuf>,
50}
51
52impl Clean {
53    /// Cleans the snarkOS node storage.
54    pub fn parse(self) -> Result<String> {
55        // Remove the specified node configuration from storage.
56        if !self.keep_node_data {
57            let node_data_dir = parse_node_data_dir(&self.node_data_storage, self.network, self.dev)?;
58            println!("{}", Self::remove_node_data(&node_data_dir)?);
59        }
60
61        // Remove the specified ledger from storage.
62        let storage_mode = match self.ledger_storage {
63            Some(path) => StorageMode::Custom(path),
64            None => match self.dev {
65                Some(id) => StorageMode::Development(id),
66                None => StorageMode::Production,
67            },
68        };
69        Self::remove_ledger(self.network, &storage_mode)
70    }
71
72    /// Removes the specified node configuration from storage.
73    fn remove_node_data(node_data_dir: &NodeDataDir) -> Result<String> {
74        // With the new layout, we can remove the entire folder.
75        let data_path = node_data_dir.path();
76
77        // Prepare the path string.
78        let path_string = format!("(in \"{}\")", data_path.display()).dimmed();
79
80        if data_path.exists() {
81            std::fs::remove_dir_all(data_path).with_context(|| format!("Failed to remove node data {path_string}"))?;
82            Ok(format!("✅ Cleaned up node data {path_string}"))
83        } else {
84            Ok(format!("✅ No node data was found {path_string}"))
85        }
86    }
87
88    /// Removes the specified ledger from storage.
89    pub(crate) fn remove_ledger(network: u16, mode: &StorageMode) -> Result<String> {
90        // Construct the path to the ledger in storage.
91        let path = aleo_std::aleo_ledger_dir(network, mode);
92
93        // Prepare the path string.
94        let path_string = format!("(in \"{}\")", path.display()).dimmed();
95
96        // Check if the path to the ledger exists in storage.
97        if path.exists() {
98            // Remove the ledger files from storage.
99            std::fs::remove_dir_all(&path)
100                .with_context(|| format!("Failed to remove the snarkOS ledger {path_string}"))?;
101            Ok(format!("✅ Cleaned the snarkOS ledger {path_string}"))
102        } else {
103            Ok(format!("✅ No snarkOS ledger was found {path_string}"))
104        }
105    }
106}