When I build small automation scripts, I often create objects to hold temporary report data—such as a customer’s draft notes, an API token, or a calculated status. Once that information is no longer valid, I do not want it sitting on the object and getting reused accidentally.
That is where the Python delattr() function helps. It removes an attribute from an object when you know the attribute name at runtime, which is especially useful in configurable scripts, data-processing jobs, and class-based applications.
In this guide, you will learn the delattr() syntax, see practical examples with complete code and output, and understand when to use it instead of del.
What Is the Python delattr() Function?
The delattr() function is a built-in Python function that deletes a named attribute from an object.
An attribute is a value attached to an object. For example, an employee object may have attributes such as name, department, and access_code.
Here is the syntax:
delattr(object, attribute_name)
The function takes two arguments:
objectis the class instance or object that contains the attributeattribute_nameis a string containing the attribute name you want to remove
Unlike the del object.attribute statement, delattr() works well when the attribute name comes from a variable, user setting, configuration file, or API response.
Python includes many useful built-in tools for everyday scripts. You can also explore other common options in this guide to Python built-in functions.
Python delattr() Function Syntax
The basic syntax looks like this:
delattr(object_name, "attribute_name")
The attribute name must be a string. Python locates that attribute on the object and removes it.
For example, imagine a small customer-reporting script. The script creates a Customer object and later removes the temporary discount_code after the report finishes.
class Customer:
def __init__(self, name, city, discount_code):
self.name = name
self.city = city
self.discount_code = discount_code
customer = Customer("Emma Carter", "Austin", "FALL25")
print("Before deletion:")
print(customer.__dict__)
delattr(customer, "discount_code")
print("\nAfter deletion:")
print(customer.__dict__)
Output:
Before deletion:
{'name': 'Emma Carter', 'city': 'Austin', 'discount_code': 'FALL25'}
After deletion:
{'name': 'Emma Carter', 'city': 'Austin'}
You can see the output in the screenshot below.

The __dict__ attribute stores an object’s writable attributes in a dictionary. It gives you an easy way to confirm what delattr() removed.
This approach works with normal Python classes. If you are still getting comfortable with object-oriented programming, start with the basics of creating a constructor in Python.
How to Use Python delattr() Step by Step
Let’s use one consistent example: a local sales-report automation script. The script creates employee report objects, adds temporary values during processing, and removes those values before saving the final result.
Create an Object with Attributes
First, create a class that represents a sales representative. A class is a blueprint for creating objects, while an object is one usable item created from that blueprint.
class SalesRepresentative:
def __init__(self, name, territory, monthly_sales):
self.name = name
self.territory = territory
self.monthly_sales = monthly_sales
representative = SalesRepresentative(
"Michael Thompson",
"California",
84500
)
print(representative.__dict__)
Output:
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500}
You can see the output in the screenshot below.

Each self.attribute_name line creates an instance attribute. An instance attribute belongs to one specific object.
For a deeper explanation of how these values differ from class-level values, see class variables vs instance variables in Python.
Delete One Attribute with delattr()
Suppose your report script adds a temporary review_status field. After a manager approves the report, you can delete that temporary value.
class SalesRepresentative:
def __init__(self, name, territory, monthly_sales):
self.name = name
self.territory = territory
self.monthly_sales = monthly_sales
representative = SalesRepresentative(
"Michael Thompson",
"California",
84500
)
representative.review_status = "Pending manager review"
print("Before deleting review_status:")
print(representative.__dict__)
delattr(representative, "review_status")
print("\nAfter deleting review_status:")
print(representative.__dict__)
Output:
Before deleting review_status:
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500, 'review_status': 'Pending manager review'}
After deleting review_status:
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500}
You can see the output in the screenshot below.

The line below does the actual work:
delattr(representative, "review_status")
Python finds the review_status attribute on representative and deletes it. If you try to read it after deletion, Python raises an AttributeError, which means the requested attribute does not exist.
Pro Tip: I have found that temporary attributes work well in small automation scripts, but I always remove them before serializing objects or sending data to another system. That prevents internal processing values from leaking into a report, JSON file, or API payload.
Use delattr() with a Dynamic Attribute Name
The biggest reason to use delattr() instead of del is that the attribute name can come from a variable.
For example, a report-cleanup script might receive a list of fields to remove from a configuration setting. The script does not know the exact attribute until runtime.
class SalesRepresentative:
def __init__(self, name, territory, monthly_sales):
self.name = name
self.territory = territory
self.monthly_sales = monthly_sales
self.internal_note = "Needs follow-up next quarter"
representative = SalesRepresentative(
"Michael Thompson",
"California",
84500
)
attribute_to_remove = "internal_note"
print("Before deletion:")
print(representative.__dict__)
delattr(representative, attribute_to_remove)
print("\nAfter deletion:")
print(representative.__dict__)
Output:
Before deletion:
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500, 'internal_note': 'Needs follow-up next quarter'}
After deletion:
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500}
You cannot write this with normal dot notation:
# This removes an attribute literally named attribute_to_remove.
del representative.attribute_to_remove
That line looks for an attribute called attribute_to_remove, not the attribute name stored inside the variable. delattr() solves that problem because it accepts the attribute name as a string.
Dynamic attribute access often appears alongside getattr() and setattr(). You can learn how those functions work together in this guide to dynamic attribute creation with getattr() and setattr().
Check an Attribute Before Calling delattr()
If the attribute does not exist, delattr() raises an AttributeError. In a one-off script, that error may be useful because it exposes a coding mistake. In a scheduled automation script, you may want to handle it cleanly instead.
Use the built-in hasattr() function to check whether an object has an attribute before deleting it.
class SalesRepresentative:
def __init__(self, name, territory, monthly_sales):
self.name = name
self.territory = territory
self.monthly_sales = monthly_sales
representative = SalesRepresentative(
"Michael Thompson",
"California",
84500
)
attribute_to_remove = "review_status"
if hasattr(representative, attribute_to_remove):
delattr(representative, attribute_to_remove)
print(f"Removed: {attribute_to_remove}")
else:
print(f"Nothing removed. '{attribute_to_remove}' does not exist.")
print(representative.__dict__)
Output:
Nothing removed. 'review_status' does not exist.
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500}
This pattern makes cleanup code safe to run more than once. That matters in scheduled jobs where the same report may rerun after an interruption.
You can also handle the error directly with try and except.
class SalesRepresentative:
def __init__(self, name, territory, monthly_sales):
self.name = name
self.territory = territory
self.monthly_sales = monthly_sales
representative = SalesRepresentative(
"Michael Thompson",
"California",
84500
)
attribute_to_remove = "review_status"
try:
delattr(representative, attribute_to_remove)
print(f"Removed: {attribute_to_remove}")
except AttributeError:
print(f"Cannot remove '{attribute_to_remove}' because it does not exist.")
print(representative.__dict__)
Output:
Cannot remove 'review_status' because it does not exist.
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500}
Use hasattr() when you expect an attribute may not exist. Use try and except when deletion is part of a larger operation that may fail for more than one reason. If you need more practice with this topic, see how to catch multiple exceptions in Python.
Remove Multiple Attributes with delattr()
A practical cleanup script often removes more than one temporary attribute. Store the names in a list and loop through them.
class SalesRepresentative:
def __init__(self, name, territory, monthly_sales):
self.name = name
self.territory = territory
self.monthly_sales = monthly_sales
self.internal_note = "Strong lead pipeline"
self.review_status = "Approved"
self.export_batch = "BATCH-2026-09"
representative = SalesRepresentative(
"Michael Thompson",
"California",
84500
)
attributes_to_remove = [
"internal_note",
"review_status",
"export_batch"
]
print("Before cleanup:")
print(representative.__dict__)
for attribute_name in attributes_to_remove:
if hasattr(representative, attribute_name):
delattr(representative, attribute_name)
print("\nAfter cleanup:")
print(representative.__dict__)
Output:
Before cleanup:
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500, 'internal_note': 'Strong lead pipeline', 'review_status': 'Approved', 'export_batch': 'BATCH-2026-09'}
After cleanup:
{'name': 'Michael Thompson', 'territory': 'California', 'monthly_sales': 84500}
This code keeps the important report fields and removes internal fields before export. The hasattr() check makes the loop resilient if one item does not exist.
For a refresher on looping through lists in Python, read how to filter lists in Python.
delattr() vs del in Python
Both delattr() and del can delete an object attribute. The right choice depends on whether you know the attribute name when writing the code.
| Situation | Use delattr() | Use del |
|---|---|---|
| Attribute name comes from a variable | Yes | No |
| Attribute name is fixed in your source code | Yes | Yes |
| You need concise code for one known attribute | No | Yes |
| You process fields from a configuration file | Yes | No |
| You build a generic cleanup function | Yes | No |
Here is the same deletion written both ways:
class Report:
def __init__(self):
self.title = "West Coast Sales Report"
self.draft_note = "Check totals before sending"
report = Report()
del report.draft_note
print(report.__dict__)
Output:
{'title': 'West Coast Sales Report'}
Here is the dynamic version:
class Report:
def __init__(self):
self.title = "West Coast Sales Report"
self.draft_note = "Check totals before sending"
report = Report()
field_name = "draft_note"
delattr(report, field_name)
print(report.__dict__)
Output:
{'title': 'West Coast Sales Report'}
Use del report.draft_note when the attribute is fixed and obvious. Use delattr(report, field_name) when your script receives the name dynamically.