It depends on why you feel the delete is necessary and what it means in the context of your application.
It can be handy, for example, to verify if there is, or isn't a Prodotto
with a certain ID. For this, an array isn't ideal because it would need to be searched. A mapping
can find it in one operation. You can also remove them and generally have fewer complications.
Consider
mapping(bytes32 => Prodotto) public prodottoStructs;
where the key is a bytes32 unique ID.
Since mappings have one vexing limitation, namely that the keys can't be enumerated, an ID list can be a handy compliment.
bytes32[] public prodottoIdList;
You push
Id values on to the list as you go. This doesn't give you a delete function, but you can do this with a boolean in the struct:
struct Prodotto {
string titolo;
address owner_address;
bool isProdotto;
}
You set the bool
to true
as you go. When you delete
, it reverts to its default false
value, which makes it easy to:
function isProdotto(bytes32 prodottoId) public view returns(bool isIndeed) {
return prodottoStructs[prodottoId].isProdotto;
}
That's not too bad.
A mapping plus a list of ID values gives you a way to quickly check for existence, count the number that exist (including deleted ones) and enumerate the data that was created. If the existence of the keys for the previously created and subsequently deleted prodottos
is problematic, then you can prune the list by moving the last key into the row to delete and shortening the list. Implicitly, that process means treating it as an unordered list of keys.
More on this: Are there well-solved and simple storage patterns for Solidity?
And the list reorganization option: https://medium.com/@robhitchens/solidity-crud-part-2-ed8d8b4f74ec
Hope it helps.