One could suggest any variation of the following:
def mydel(regexp):
targets = globals().copy()
for target in targets:
if re.match(regexp, target):
globals().pop(target)
This will update the globals() dictionary which keeps track of what variables are accessible.
Now, you asked about memory management, so the question remains whether removing a variable from the globals() dictionary would actually trigger it to be deleted.
For this we can check that the lifetime of the object pointed to by the variable we deleted has actually ended. This can be done in cpython by importing ctypes.
import ctypes
h1, h2, h3 = 1, 2, 3
id_check = id(h1)
mydel(r'h.*')
Trying to use any of the variables shows that it is surely, as expected, not accessible:
h1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/var/folders/81/h94bb5fx2cs6chrwgdsvn4cr0000gp/T/ipykernel_70978/1500632009.py in <module>
----> 1 h3
NameError: name 'h3' is not defined
However, if we use ctypes to query for the object id...
ctypes.cast(id_check, ctypes.py_object).value
1
Which shows that the memory has NOT been freed.