Repository: PMEAL/OpenPNM

Issue:
Add the ability to delete hirarchical dictionary keys in one go
# Intended
```python
del geo['throat.conduit_lengths']  # this currently doesn't work
```
# Current
```python
del geo['throat.conduit_lengths.pore1']
del geo['throat.conduit_lengths.pore2']
del geo['throat.conduit_lengths.throat']
```

Discussion:

Patch:
diff --git a/openpnm/core/Base.py b/openpnm/core/Base.py
index d1e2840ad..7b66f3993 100644
--- a/openpnm/core/Base.py
+++ b/openpnm/core/Base.py
@@ -251,6 +251,14 @@ class Base(dict):
             raise KeyError(key)
         return vals
 
+    def __delitem__(self, key):
+        try:
+            super().__delitem__(key)
+        except KeyError as e:
+            d = self[key]  # if key is a nested dict, get all values
+            for item in d.keys():
+                super().__delitem__(item)
+
     def _set_name(self, name, validate=True):
         old_name = self.settings['name']
         if name == old_name:


Test patch:
diff --git a/tests/unit/core/BaseTest.py b/tests/unit/core/BaseTest.py
index f82d99875..18ed0ae18 100644
--- a/tests/unit/core/BaseTest.py
+++ b/tests/unit/core/BaseTest.py
@@ -930,6 +930,19 @@ class BaseTest:
         with pytest.raises(KeyError):
             pn.get_conduit_data('blah')
 
+    def test_del_nested_dicts(self):
+        pn = op.network.Cubic(shape=[3, 3, 3])
+        geo = op.geometry.SpheresAndCylinders(network=pn,
+                                              pores=pn.Ps,
+                                              throats=pn.Ts)
+        assert 'throat.hydraulic_size_factors.pore1' in geo.keys()
+        del geo['throat.hydraulic_size_factors']
+        assert 'throat.hydraulic_size_factors.pore1' not in geo.keys()
+        with pytest.raises(KeyError):
+            geo['throat.hydraulic_size_factors']
+        with pytest.raises(KeyError):
+            del geo['pore.blah']
+
 
 if __name__ == '__main__':