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 byfilter()orall().
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
DoesNotExisterror if you useget()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
| Operation | Code Example | Description |
|---|---|---|
| Delete single object | obj = Model.objects.get(id=1) obj.delete() | Deletes one object fetched by primary key or filter. |
| Delete multiple objects | Model.objects.filter(field=value).delete() | Deletes all objects matching the filter. |
| Delete all objects | Model.objects.all().delete() | Deletes every object in the model (use carefully). |
| Handle missing object | try: obj = Model.objects.get(id=999) obj.delete() except Model.DoesNotExist: pass | Avoid 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.