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 snarkos_node::bft::helpers::proposal_cache_path;
17
18use aleo_std::StorageMode;
19use anyhow::{Result, bail};
20use clap::Parser;
21use colored::Colorize;
22use std::path::PathBuf;
23
24/// Cleans the snarkOS node storage.
25#[derive(Debug, Parser)]
26pub struct Clean {
27    /// Specify the network to remove from storage.
28    #[clap(default_value = "0", long = "network")]
29    pub network: u16,
30    /// Enables development mode, specify the unique ID of the local node to clean.
31    #[clap(long)]
32    pub dev: Option<u16>,
33    /// Specify the path to a directory containing the ledger. Overrides the default path (also for
34    /// dev).
35    #[clap(long = "path")]
36    pub path: Option<PathBuf>,
37}
38
39impl Clean {
40    /// Cleans the snarkOS node storage.
41    pub fn parse(self) -> Result<String> {
42        // Initialize the storage mode.
43        let storage_mode = match self.path {
44            Some(path) => StorageMode::Custom(path),
45            None => match self.dev {
46                Some(id) => StorageMode::Development(id),
47                None => StorageMode::Production,
48            },
49        };
50
51        // Remove the current proposal cache file, if it exists.
52        let proposal_cache_path = proposal_cache_path(self.network, &storage_mode);
53        if proposal_cache_path.exists() {
54            if let Err(err) = std::fs::remove_file(&proposal_cache_path) {
55                bail!("Failed to remove the current proposal cache file at {}: {err}", proposal_cache_path.display());
56            }
57        }
58        // Remove the specified ledger from storage.
59        Self::remove_ledger(self.network, &storage_mode)
60    }
61
62    /// Removes the specified ledger from storage.
63    pub(crate) fn remove_ledger(network: u16, mode: &StorageMode) -> Result<String> {
64        // Construct the path to the ledger in storage.
65        let path = aleo_std::aleo_ledger_dir(network, mode);
66
67        // Prepare the path string.
68        let path_string = format!("(in \"{}\")", path.display()).dimmed();
69
70        // Check if the path to the ledger exists in storage.
71        if path.exists() {
72            // Remove the ledger files from storage.
73            match std::fs::remove_dir_all(&path) {
74                Ok(_) => Ok(format!("✅ Cleaned the snarkOS node storage {path_string}")),
75                Err(error) => {
76                    bail!("Failed to remove the snarkOS node storage {path_string}\n{}", error.to_string().dimmed())
77                }
78            }
79        } else {
80            Ok(format!("✅ No snarkOS node storage was found {path_string}"))
81        }
82    }
83}