Skip to main content

nu_command/misc/
unlet.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct DeleteVar;
5
6impl Command for DeleteVar {
7    fn name(&self) -> &str {
8        "unlet"
9    }
10
11    fn description(&self) -> &str {
12        "Delete variables from nushell memory, making them unrecoverable."
13    }
14
15    fn signature(&self) -> nu_protocol::Signature {
16        Signature::build("unlet")
17            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
18            .rest(
19                "rest",
20                SyntaxShape::Any,
21                "The variables to delete (pass as $variable_name).",
22            )
23            .category(Category::Experimental)
24    }
25
26    fn run(
27        &self,
28        _engine_state: &EngineState,
29        _stack: &mut Stack,
30        _call: &Call,
31        _input: PipelineData,
32    ) -> Result<PipelineData, ShellError> {
33        // Compiled specially by the IR compiler (`compile_unlet`). This path is never used
34        // when running in IR mode.
35        eprintln!(
36            "Tried to execute 'run' for the 'unlet' command: this code path should never be reached in IR mode"
37        );
38        unreachable!()
39    }
40
41    fn examples(&self) -> Vec<Example<'_>> {
42        vec![
43            Example {
44                example: "let x = 42; unlet $x",
45                description: "Delete a variable from memory.",
46                result: None,
47            },
48            Example {
49                example: "let x = 1; let y = 2; unlet $x $y",
50                description: "Delete multiple variables from memory.",
51                result: None,
52            },
53            Example {
54                example: "unlet $nu",
55                description: "Attempting to delete a built-in variable fails.",
56                result: None,
57            },
58            Example {
59                example: "unlet 42",
60                description: "Attempting to delete a non-variable fails.",
61                result: None,
62            },
63        ]
64    }
65}