Python does not have a direct command or function like “unset” to remove a variable from memory or its current scope, but you can use the del statement, reassign a variable to None, or let variables go out of scope.
The main question is, why do you want to delete a variable? Because you want to free your memory, avoid namespace clutter, or prevent unintended use of stale data. It will help you improve your system’s performance by removing unnecessary variables.
Method 1: Using del
To explicitly delete a variable or an object from the current namespace, always use the recommended “del” statement. It will free the memory if no other references exist.
Here, one thing to note is that it completely deletes the variable from the system. It does not assign the variable to None.
If the object has no other references, Python’s garbage collector may reclaim the memory.
var = 10 print(var) # Output: 10 del var try: print(var) # Raises NameError: name 'var' is not defined except NameError as e: print(e) # Output: name 'var' is not defined
First, we declared a variable with the value 10. Then, we unset that variable by using del.
After removing it, even if we try to access that variable, it will throw a NameError, stating that the name “var” is not defined because it no longer exists in the system.
Freeing memory for large objects
What if we are working with extremely large objects, and we no longer need them in our memory
Large data structures, such as lists or data frames, tend to be larger when working with machine learning tasks. Unsetting them can help manage resources.
import sys humongous_list = [0] * 10**8 # 100M elements print(sys.getsizeof(humongous_list)) # Output: 800000056 (~800MB) del humongous_list try: print(humongous_list) # Raises NameError except NameError: print("Variable unset") # Output: Variable unset
In the above program, we created a list with 100 million elements and then deleted it, potentially allowing memory reclamation.
Multiple references to an Object
What if your object has multiple references? If you delete one reference, what about the other references?
If an object has multiple references, del only removes one reference. The object persists until all references to it are removed.
origin = [1, 2, 3] duplicate = origin # Same object del origin print(duplicate) # Output: [1, 2, 3] del duplicate print(origin) # Raises NameError: name 'origin' is not defined
Deleting non-existent variables
What if you try to unset a variable that does not exist? Which means it has not been defined, and yet you are trying to remove that? The answer is that it will throw NameError.
try: del not_exist_var # Raises NameError except NameError as e: print(e) # Output: name 'not_exist_var' is not defined
Unsetting multiple variables
You can unset multiple variables in a single statement. You don’t need any loop or other method to do that.
x, y, z = 11, 21, 31 del x, y print(z) # Output: 31 print(x) # Output: NameError: name 'x' is not defined
We deleted two variables, x and y, from the memory in a single del statement, which is very efficient.
Method 2: Reassigning to None
Another way is to reassign a variable to None, which means you are saying that it’s no longer needed, but the variable name remains in the namespace. The variable still exists but points to None.
list_var = [1, 2, 3] print(list_var) # Output: [1, 2, 3] list_var = None print(list_var) # Output: None
The original list [1, 2, 3] may be garbage-collected if no other references to it remain.
Method 3: Letting variables go out of scope
When you use variables inside a function, they are automatically unset when their scope (e.g., a function or block) ends.
Underneath, you can understand this: variables defined in a local scope are removed when the scope exits, unless they are referenced elsewhere.
def unset_main(): var = 21 print(var) # Output: 21 unset_main() try: print(var) # Raises NameError: name 'var' is not defined except NameError as e: print(e) # Output: name 'var' is not defined
That’s all!