Python delattr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, delattr(obj, "name") removes an attribute and does the same as del obj.name, but with the name given as a string. Afterwards, reading that attribute raises AttributeError, unless a class attribute with the same name shows through.

It is useful for cleanup code where names come from data, for example removing all temporary attributes that start with tmp_. It also works on classes, which removes the attribute for every instance that does not have its own copy.

delattr() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass the object and the attribute name.

python
delattr(obj, "name")
CallWhat it does
delattr(p, "nickname")Same as del p.nickname.
delattr(obj, name)Name taken from a variable.
delattr(Class, "x")Removes a class attribute.
delattr(p, "missing")Raises AttributeError.

Deleting an instance attribute can reveal a class attribute of the same name.

Library declarations
python
delattr(object, name, /)

Parameters

ParameterDescription
objectRequired. The object to modify.
nameRequired. The attribute name as a string.

Return Value

Return ValueDescription
NoneNothing; the attribute is removed.

Python Examples

Example 1 — Delete an Attribute and Try Again

Remove one attribute, check the result, and delete it a second time.

python
class Profile:
    def __init__(self):
        self.name = "Ana"
        self.nickname = "A"


p = Profile()
delattr(p, "nickname")
print(hasattr(p, "nickname"))
print(vars(p))
try:
    delattr(p, "nickname")
except AttributeError:
    print("nickname is already gone")

How It Works

  1. Removed. After delattr(), hasattr() reports False.
  2. Gone from __dict__. Only name remains in the instance dictionary.
  3. Second delete fails. There is nothing left to remove, so Python raises AttributeError.
  4. Same as del. del p.nickname would behave identically.

Example 2 — Remove All Temporary Attributes

Find attribute names with a prefix and delete them in a loop.

python
class Cache:
    def __init__(self):
        self.users = [1, 2]
        self.tmp_page = "<html>"
        self.tmp_query = "SELECT"


c = Cache()
for name in [n for n in vars(c) if n.startswith("tmp_")]:
    delattr(c, name)
print(sorted(vars(c)))

How It Works

  1. Names as data. vars(c) lists the attribute names.
  2. Copy before deleting. The list comprehension builds a separate list, so the dict is not changed while it is being looped over.
  3. Dynamic delete. delattr() takes each name from the loop variable.
  4. What remains. Only users survives the cleanup.

Example 3 — Instance Attributes Shadow Class Attributes

Delete an instance attribute to reveal the class value, then delete the class attribute.

python
class Theme:
    color = "blue"


t = Theme()
t.color = "red"
print(t.color)
delattr(t, "color")
print(t.color)

try:
    delattr(t, "color")
except AttributeError:
    print("the instance has no color of its own now")

delattr(Theme, "color")
print(hasattr(t, "color"))

How It Works

  1. Shadowing. Setting t.color hides the class value "blue".
  2. Revealed. Deleting the instance attribute makes t.color fall back to the class.
  3. Not the class attribute. A second delattr(t, ...) fails: the instance has nothing left to delete.
  4. Delete on the class. delattr(Theme, "color") removes it for every instance.

Frequently Asked Questions

It deletes an attribute by name: delattr(obj, "x") is the same as del obj.x.
When the attribute name is in a variable or comes from data.
The object has no such attribute of its own. Class attributes must be deleted from the class.
Yes, from the class: delattr(MyClass, "method"). Instances normally do not own their methods.
None.

Next: dict()

Build a dictionary of key/value pairs.

dict() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful