Skip to main content

Command Palette

Search for a command to run...

Delete Operator

Published
2 min readView as Markdown
Delete Operator

Delete Operator

  1. In Solidity, the delete operator is used to reset a variable to its default value. The delete operator can be used on any state variable or on an element of an array or a mapping type.

  2. When used on a variable, the delete operator 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:

  1. Resetting a variable to its default value:
uint256 myVar = 42;
delete myVar; // myVar is now equal to 0
  1. Resetting an element of an array to its default value:
uint256[] myArray = [1, 2, 3];
delete myArray[1]; // myArray is now [1, 0, 3]
  1. 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.

More from this blog