Python
del
delattr
programming
code optimization

Which is better in python, del or delattr?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

del and delattr() are not really competitors in Python. del is a language statement that can delete names, items, slices, and attributes, while delattr() is a built-in function specifically for deleting an attribute from an object when the attribute name is available as a string.

What del Can Do

del is more general. It works with several deletion forms depending on the target.

python
values = [10, 20, 30]
del values[1]
print(values)

It can remove:

  • a variable binding such as del x
  • a list item such as del values[1]
  • a dictionary entry such as del data["key"]
  • an object attribute such as del obj.name

That breadth is why del is the default tool most of the time. It maps directly to Python syntax and fits naturally when the target is known in source code.

What delattr() Does

delattr(obj, name) only deletes an attribute from an object.

python
1class User:
2    def __init__(self):
3        self.email = "[email protected]"
4
5
6user = User()
7delattr(user, "email")
8print(hasattr(user, "email"))

The main advantage is that the attribute name can be dynamic.

python
field_name = "email"
delattr(user, field_name)

You cannot write del user.field_name and expect it to delete email; that would target the literal attribute named field_name. When the attribute name comes from data, configuration, or user input, delattr() is the correct tool.

Attribute Deletion Semantics

For attribute removal, del obj.attr and delattr(obj, "attr") are equivalent in spirit. Both end up using Python's attribute deletion machinery, including custom __delattr__ methods if the object defines one.

python
1class Demo:
2    def __init__(self):
3        self.value = 42
4
5    def __delattr__(self, name):
6        print(f"Deleting {name}")
7        super().__delattr__(name)
8
9
10item = Demo()
11del item.value

That means the difference is about syntax and use case, not about a fundamentally different deletion mechanism.

Which One Is Better?

If the attribute name is written directly in code, del obj.attr is usually clearer and more idiomatic.

python
del user.email

If the attribute name is computed at runtime, delattr() is the better and often the only practical choice.

python
for field in ["email", "phone"]:
    if hasattr(user, field):
        delattr(user, field)

So the real rule is simple:

  • use del for normal direct deletion
  • use delattr() for dynamic attribute names

What Neither Tool Does

Neither del nor delattr() guarantees immediate object destruction. They remove a binding or attribute reference. Actual memory reclamation depends on Python's reference counting and garbage collection behavior.

That matters because some developers think del x means "free this object right now." It really means "remove this reference." If other references still exist, the object remains alive.

Common Pitfalls

The biggest pitfall is confusing attribute deletion with name deletion. del x removes a name from the current namespace, while delattr(obj, "x") removes an attribute from obj.

Another issue is using delattr() when the attribute name is static. That works, but it is noisier than del obj.attr and less readable.

Developers also sometimes expect del obj.missing and delattr(obj, "missing") to silently do nothing. They do not. Both raise AttributeError if the attribute is absent, unless custom object logic changes that behavior.

Finally, avoid using either one as a manual memory-management habit. In normal Python code, object lifetime should usually be handled by scope and reference ownership rather than scattered deletion statements.

Summary

  • 'del is a general deletion statement for names, items, slices, and attributes.'
  • 'delattr() is specifically for deleting an attribute by name.'
  • Use del obj.attr when the attribute is known directly in code.
  • Use delattr(obj, name) when the attribute name is dynamic.
  • Neither form guarantees immediate memory release; they only remove references or attributes.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.