Use Python’s built-in delattr(object, name) to delete a named attribute when the object allows it. For a fixed attribute, it is equivalent to del object.name; the function form is handy when the name is stored in a string variable.
How to remove an attribute from an object
Give delattr() the object and a string containing the attribute name:
class User:
pass
user = User()
user.nickname = "Mira"
delattr(user, "nickname")
After the call succeeds, the nickname attribute has been deleted. Python’s Built-in Functions reference for Python 3.14.8 describes delattr() as a relative of setattr() and equates delattr(x, 'foobar') with del x.foobar.
When to use delattr() instead of del
| Form | Best suited to | Example |
|---|---|---|
del object.name |
A fixed, conventional attribute name that is clear in the code. | del user.nickname |
delattr(object, name) |
A name held in a variable or chosen at runtime. | delattr(user, attribute_name) |
For example, when a string variable determines which attribute to remove:
#1 Best Overall
attribute_name = "nickname"
delattr(user, attribute_name)
The name argument must be a string, but it does not have to be a valid Python identifier. That lets delattr() address attribute names that cannot be written after a dot in Python source; do not try to express such a name as del object.some-name, which is not valid attribute-deletion syntax.
Why deletion can behave differently between objects
delattr() does not guarantee that every named attribute can be removed. Attribute deletion is governed by the object’s behavior. A class can customize it with __delattr__(self, name), the data-model hook analogous to __setattr__. Python’s data model documentation says this method should be implemented when del obj.name has meaningful behavior for that object. It also notes that certain sensitive deletions raise an auditing event called object.__delattr__; this is not documented as applying to every deletion.
Rank #2
Descriptors can also participate. A descriptor’s __delete__(self, instance) method is called when its managed attribute is deleted. As a result, deletion may run custom logic or be rejected rather than simply removing a key from an instance dictionary. The Python Descriptor Guide explains this protocol and notes that properties use descriptor machinery.
Errors and checks
If deletion is not supported or the requested attribute is unavailable, the operation can fail; custom deletion behavior may also raise an exception. A prior hasattr(object, name) check does not guarantee that a subsequent delattr(object, name) will succeed, because deletion behavior can be customized.
Deleting an attribute is not the same as deleting the object. delattr() removes a named attribute reference according to the object’s rules; it does not itself promise to destroy the object or free its memory.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




