0
0
DjangoHow-ToBeginner · 4 min read

How to Delete Objects in Django ORM: Syntax and Examples

In Django ORM, you delete objects by calling the delete() method on a model instance or a queryset. For example, obj.delete() removes a single object, while Model.objects.filter(...).delete() deletes multiple objects matching the filter.
📐

Syntax

The delete() method is used to remove objects from the database. You can call it on a single model instance or on a queryset to delete multiple records.

  • Single object deletion: Call delete() on a model instance.
  • Multiple objects deletion: Call delete() on a queryset returned by filter() or all().
python
obj = Model.objects.get(id=1)
obj.delete()

Model.objects.filter(field=value).delete()
💻

Example

This example shows how to delete a single object and multiple objects from a Django model called Book. It demonstrates fetching objects and deleting them safely.

python
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)

# Deleting a single object
book = Book.objects.create(title='Django Basics')
print(f'Created book: {book.title}')

book.delete()
print('Deleted the single book object.')

# Deleting multiple objects
Book.objects.create(title='Django Advanced')
Book.objects.create(title='Django Tips')

deleted_count, _ = Book.objects.filter(title__icontains='Django').delete()
print(f'Deleted {deleted_count} books with "Django" in the title.')
Output
Created book: Django Basics Deleted the single book object. Deleted 3 books with "Django" in the title.
⚠️

Common Pitfalls

Common mistakes when deleting objects in Django ORM include:

  • Calling delete() on a queryset without filtering, which deletes all records.
  • Not handling related objects that may also be deleted due to cascading.
  • Trying to delete an object that does not exist, which raises DoesNotExist error if you use get() before deleting.

Always filter carefully and handle exceptions when deleting.

python
try:
    obj = Model.objects.get(id=999)  # May not exist
    obj.delete()
except Model.DoesNotExist:
    print('Object not found, cannot delete.')

# Wrong: deletes all objects
# Model.objects.all().delete()  # Use with caution!
Output
Object not found, cannot delete.
📊

Quick Reference

OperationCode ExampleDescription
Delete single objectobj = Model.objects.get(id=1) obj.delete()Deletes one object fetched by primary key or filter.
Delete multiple objectsModel.objects.filter(field=value).delete()Deletes all objects matching the filter.
Delete all objectsModel.objects.all().delete()Deletes every object in the model (use carefully).
Handle missing objecttry: obj = Model.objects.get(id=999) obj.delete() except Model.DoesNotExist: passAvoid errors when object does not exist.

Key Takeaways

Use the delete() method on model instances or querysets to remove objects.
Filtering querysets before delete() prevents accidental removal of all records.
Handle DoesNotExist exceptions when deleting single objects fetched by get().
Deleting a queryset returns the count of deleted objects and related deletions.
Be cautious of cascading deletes affecting related objects automatically.