How to delete a record in Django models?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Django, deleting a record means deleting a model instance through the ORM. The basic API is simple, but the real behavior depends on whether you delete one instance or a queryset, and on how related models are configured through ForeignKey and on_delete.
Delete a Single Model Instance
The most direct pattern is:
delete() removes that row from the database and returns a tuple describing how many objects were deleted and which model types were affected.
If the object may not exist, use exception handling:
In a view, get_object_or_404() is often cleaner:
Delete Multiple Records with a QuerySet
If you want to delete more than one row, call delete() on a queryset:
This is efficient because Django can translate it into a bulk delete operation. It is usually better than fetching each row and deleting them one by one unless you explicitly need per-object custom logic.
That distinction matters because bulk deletion may bypass some instance-level behavior you expected from loading each object manually.
Understand Related Object Deletion
Deletion behavior becomes more important when other models point to the record. Consider:
If you delete an Author, Django will also delete related Article rows because on_delete=models.CASCADE says dependent rows should be removed.
Other common on_delete behaviors include:
- '
PROTECT, which blocks the delete' - '
SET_NULL, which clears the relation if the field allowsnull=True' - '
SET_DEFAULT, which assigns a default value'
Do not treat deletion as an isolated operation. Check the relation graph first.
Wrap Important Deletes in a Transaction
If deletion is part of a larger workflow, use a transaction so the database stays consistent:
This is especially useful when you combine deletes with updates, logging records, or follow-up inserts that must succeed or fail together.
Soft Delete Versus Hard Delete
In many business systems, a hard delete is not the best choice. You may need auditability or the ability to restore records later. A common alternative is soft delete, where the row stays in the table and you mark it as inactive.
That design changes queries too, because active views must filter out deleted rows consistently.
Common Pitfalls
The biggest mistake is deleting a parent object without checking related rows. CASCADE can remove more data than expected.
Another problem is confusing queryset deletion with instance deletion. Queryset deletes are efficient, but if you expected custom per-instance code or signal behavior tied to loading objects individually, verify that your approach matches that requirement.
Developers also sometimes expose delete actions through GET requests. That is unsafe. Destructive actions should normally be triggered through POST and protected with CSRF validation.
Finally, remember that delete() is permanent unless you designed a recovery path. If the application needs reversibility, use soft delete or database backups rather than hoping the row can be reconstructed later.
Summary
- Delete one object with
instance.delete(). - Delete many objects with
queryset.delete(). - Check
ForeignKeyrelationships andon_deletebehavior before removing data. - Use transactions when deletes are part of a larger workflow.
- Prefer soft delete when the business domain requires recovery or auditing.
Related reading
- How to delete a specific line in a text file using Python?
- How to delete an item in a list if it exists?
- How to delete items from a dictionary while iterating over it?
- How to delete last item in list?
- How to delete rows from a pandas DataFrame based on a conditional expression
- How to delete the last row of data of a pandas dataframe
- How to determine if a list is subset of another list?
- How to determine if a number is any type of int core or numpy, signed or not?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.