Cleaning the canister

Is there a way to completely clear the canister of data?
Or let’s say there is a possibility of re-creation with an already known canister_id.?
It doesn’t really matter to me whether the logic remains there(.wasm) - that is, complete cleaning.

If you want to reinstall code to a canister and wipe all it’s state you can use the --mode reinstall flag

If you just want to wipe code and state use dfx canister uninstall-code


Thanks. OK, I have already used these operators in the project. Let’s say my code is below:

    public shared({caller}) func reinstall_wasm(
        canister_id : Principal, 
        wasm : Wasm) : async Bool {
                await actor_ic.install_code({arg = []; wasm_module = wasm; mode = #reinstall; canister_id = canister_id;});
                return true;
            catch e { 
                Debug.print("reinstall_wasm: error: " # debug_show(Error.message(e)));
                return false; 

Completely clean the canister?

I understand correctly that the code:

    public func get_rts_memory_size(): async Nat {  
        return Prim.rts_memory_size();

Will it return the number of bytes of an almost empty canister? Or will the byte size be equal to the already marked area before cleaning?

Depends on your definition of ‘completely clean’. This will clean the canister and then install the new wasm module. So it will be like a new canister that you installed code into for the first time. If you want a completely empty canister, you’ll have to use the uninstall_code function.

Thanks. I have not fully described the context of the question. I need to clarify. The fact is that I use the data saving functions when updating the code in the canister.

 system func preupgrade(){  // save logic };
 system func postupgrade(){ // save logic  };

That is, I need to definitely (without taking into account the operators above) delete all the data. It would be great if there was such a function. Thanks anyway. I will need to check everything in my tests.