Delete Operator

Delete Operator
In Solidity, the
deleteoperator is used to reset a variable to its default value. Thedeleteoperator can be used on any state variable or on an element of an array or a mapping type.When used on a variable, the
deleteoperator resets the value of the variable to its default value, which is zero for numeric types, false for boolean types, and an empty array or mapping for complex types.
Here are some examples of how the delete operator can be used in Solidity:
- Resetting a variable to its default value:
uint256 myVar = 42;
delete myVar; // myVar is now equal to 0
- Resetting an element of an array to its default value:
uint256[] myArray = [1, 2, 3];
delete myArray[1]; // myArray is now [1, 0, 3]
- Resetting a key-value pair in a mapping to its default value:
mapping(uint256 => string) myMapping;
myMapping[1] = "Hello";
delete myMapping[1]; // myMapping[1] is now an empty string
It's important to note that the delete operator does not actually free up any storage space in Solidity. It simply resets the variable or element to its default value. To free up storage space, you need to use the selfdestruct function, which destroys the contract and returns any remaining ether to a specified address.




