How to destroy an object in Python?

When an object is deleted or destroyed, a destructor is invoked. Before terminating an object, cleanup tasks like closing database connections or file handles are completed using the destructor.

The garbage collector in Python manages memory automatically. For instance, when an object is no longer relevant, it clears the memory.

In Python, the destructor is entirely automatic and never called manually. In the following two scenarios, the destructor is called ?

  • When an object is no longer relevant or it goes out of scope
  • The object's reference counter reaches zero

Using the __del__() Method

In Python, a destructor is defined using the special method __del__(). When we run del object_name, the object's destructor is automatically called, and it then gets garbage collected.

Example 1

Following is an example of destructor using __del__() method ?

# creating a class named destructor
class destructor:
    # initializing the class
    def __init__(self):
        print("Object gets created")

    # calling the destructor
    def __del__(self):
        print("Object gets destroyed")

# create an object
obj = destructor()

# deleting the object
del obj

The output of the above code is ?

Object gets created
Object gets destroyed

Note ? The destructor in the code above is invoked either after the program has finished running or when references to the object are deleted. This indicates that the object's reference count drops to zero now rather than when it leaves the scope.

Example 2

In the following example, we will use Python's del keyword for destroying user-defined classes ?

class destructor:
    Numbers = 10
    def formatNumbers(self):
        return "@" + str(self.Numbers)

del destructor
print(destructor)

The output of the above code is ?

NameError: name 'destructor' is not defined

We get the above error as the 'destructor' class gets destroyed.

Example 3

In the following example, we will see how to use the destructor and when the destructor is called when we delete the object ?

class destructor:
    # using constructor
    def __init__(self, name):
        print('Inside the Constructor')
        self.name = name
        print('Object gets initialized')
    
    def show(self):
        print('The name is', self.name)
        
    # using destructor
    def __del__(self):
        print('Inside the destructor')
        print('Object gets destroyed')
        
# creating an object
d = destructor('Destroyed')
d.show()

# deleting the object
del d

The output of the above code is ?

Inside the Constructor
Object gets initialized
The name is Destroyed
Inside the destructor
Object gets destroyed

Reference Counting and Destructor

Key points about destructors in Python ?

  • When an object's reference count reaches 0, the __del__ method is invoked for that object
  • When the application closes or we manually remove all references using the del keyword, the reference count for that object drops to zero
  • When we delete an object reference, the destructor won't be called until all references to the object are removed

Example

Let's use an example to understand the above principles. This example shows that destructors only function when all references to the object are destroyed ?

import time

class destructor:
    # using constructor
    def __init__(self, name):
        print('Inside the Constructor')
        self.name = name
        print('Object gets initialized')
    
    def show(self):
        print('The name is', self.name)
        
    # using destructor
    def __del__(self):
        print('Inside the destructor')
        print('Object gets destroyed')
        
# creating an object
d = destructor('Destroyed')

# Creating a new reference where both references point to same object
d1 = d
d.show()

# deleting the object d (but d1 still references it)
del d

# adding sleep and observing the output
time.sleep(2)
print('After sleeping for 2 seconds')
d1.show()

# Now delete the last reference
del d1
print('All references deleted')

The output shows that destructors are only called after all references to the objects are removed ?

Inside the Constructor
Object gets initialized
The name is Destroyed
After sleeping for 2 seconds
The name is Destroyed
Inside the destructor
Object gets destroyed
All references deleted

Conclusion

The __del__() method in Python provides automatic cleanup when objects are destroyed. Remember that destructors are called only when an object's reference count reaches zero, not when individual references are deleted.

Updated on: 2026-03-24T19:40:48+05:30

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements