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.
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.
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.
The main advantage is that the attribute name can be dynamic.
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.
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.
If the attribute name is computed at runtime, delattr() is the better and often the only practical choice.
So the real rule is simple:
- use
delfor 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
- '
delis a general deletion statement for names, items, slices, and attributes.' - '
delattr()is specifically for deleting an attribute by name.' - Use
del obj.attrwhen 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
- Which is better On log n or On2
- Which is better way to calculate nCr
- Which is faster clear collection or instantiate new
- Which is faster, \`Hash\` lookup or Binary search?
- Which is faster in Python x.5 or math.sqrtx?
- Which is the preferred way to concatenate a string in Python?
- Which is faster multiple single INSERTs or one multiple-row INSERT?
- Which is fastest? SELECT SQL_CALC_FOUND_ROWS FROM table, or SELECT COUNT

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 courseTrack 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.