Python: Delete objects using del statement

Python Tutorials

Summary: In this tutorial, we will learn what is del in Python, how it works, and how can we use it to delete objects in Python.

What is Del in Python?

The del is a statement in Python that deletes objects. Since everything in Python is an object, whether it is a variable, function, class, etc., we can easily remove them from existence using the del statement.

For example, here we are deleting a variable using the del statement:

>>> var = 'Awesome'
>>> del var
>>> print(var)
NameError: name 'var' is not defined

After deleting if we try to access the variable, we get an error stating that the variable has not been defined.

Similarly, we can also delete user-defined class and objects using the del keyword in Python.

For instance, in the following code, we are first defining a class and then deleting it using the del keyword:

>>> class Student:
...     pass
>>> del Student
>>> obj = Student()
NameError: name 'Student' is not defined

Since we deleted the class, we cannot instantiate it to create its objects.

How does dell work in Python, does it Free the Memory?

The object which we intend to delete could be a variable, function, instance of a class, etc., which isn’t a value, but a name for the value that is stored in the memory.

The del statement removes the binding of this object with its value, which disassociates them.

In doing so, the value becomes unreachable and the object is removed from the namespace, but it doesn’t necessarily remove the value from the memory.

If the object which we are deleting is the only reference to the value, then only the value will be garbage collected.

If there are multiple references for the same value, deleting one of them will not clear the memory.

Examples using del in Python

Example 1: Delete a function using del

>>> def myfun():
...     print('myfun')
>>> del myfun
>>> myfun()
NameError: name 'myfun' is not defined

Example 2: Delete attribute of an object using del

>>> class Myclass:
...     def __init__(self):
...             self.a = 'A'
...             self.b = 'B'
>>> obj = Myclass()
>>> del obj.b
>>> print(obj.a)
>>> print(obj.b)
AttributeError: 'Myclass' object has no attribute 'b'

We can also delete items from the Python data structures such as list, dictionary, etc., using the del statement. Let’s see some examples of this.

Example 3: Delete items from a list using del

>>> l = [1, 2, 3, 4, 5, 6, 7]
>>> del l[3:]
>>> print(l)
[1, 2, 3]

Example 4: Delete items from a dictionary using del

>>> d= {'key1': 'value1', 'key2': 'value2'}
>>> del d['key1']
>>> print(d)
{'key2': 'value2'}


The del statement in Python is used to delete the objects in the current namespace. The object which we intend to delete could be anything in Python, for example, a class, variable, items of a list or dictionary, etc.

Deleting any object using del doesn’t necessarily clear the memory, unless we are deleting the only reference to the object.

Leave a Reply

Your email address will not be published. Required fields are marked *