Creating a Basic Delete API with Django REST Framework
Last updated 4 months, 3 weeks ago | 401 views 75 5

A Delete API allows clients to remove an existing resource from the database. In RESTful design, this corresponds to the DELETE
HTTP method.
In this guide, you’ll learn how to build a DELETE /books/<id>/
endpoint to delete a book using Django REST Framework.
What is a Delete API?
A Delete API enables the removal of a specific object by its identifier (commonly the primary key).
Example:
DELETE /books/1/
Response:
204 No Content
The 204
status code indicates the resource was deleted successfully and there’s no additional content to return.
Step-by-Step: Build a Delete API
Let’s walk through building a Delete endpoint using DestroyAPIView
provided by DRF.
1. Ensure You Have the Book Model
# books/models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
def __str__(self):
return self.title
Run migrations if you haven't:
python manage.py makemigrations
python manage.py migrate
2. Create the Serializer (for completeness)
# books/serializers.py
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
3. Create the DeleteAPIView
# books/views.py
from rest_framework.generics import DestroyAPIView
from .models import Book
from .serializers import BookSerializer
class BookDeleteView(DestroyAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
✅ DestroyAPIView
handles the DELETE
method and removes an object by its primary key.
4. Add the URL Route
# books/urls.py
from django.urls import path
from .views import BookDeleteView
urlpatterns = [
path('books/<int:pk>/delete/', BookDeleteView.as_view(), name='book-delete'),
]
Include the app routes in your main project:
# myapi/urls.py
from django.urls import path, include
urlpatterns = [
path('api/', include('books.urls')),
]
✅ Testing the Delete API
Start the server:
python manage.py runserver
Use Postman, curl, or DRF’s web interface:
DELETE Request:
DELETE http://127.0.0.1:8000/api/books/1/delete/
Response:
204 No Content
If the object with ID 1 exists, it will be deleted. If not:
{
"detail": "Not found."
}
Summary
Feature | Description |
---|---|
DestroyAPIView |
Used to delete model instances |
DELETE method |
Triggers the deletion |
204 No Content |
Standard response after successful deletion |
<int:pk> |
Identifies which object to delete |
⚠️ Common Pitfalls
Problem | Fix |
---|---|
404 Not Found | Ensure the object with that ID exists |
Permission errors | Check your view permissions if you've set them |
Method Not Allowed | Make sure the view is bound to the DELETE method |
Tips
-
Use Django Admin to create test entries.
-
Protect delete operations with authentication and permissions.
-
Never allow delete operations in production without strict access control.
What's Next?
You now have a fully functional Delete API. Here’s where you can go from here:
-
✅ Combine all CRUD operations in a
ModelViewSet
-
Add authentication and permissions for safe access
-
Use routers for clean URL configuration
-
Add unit tests for your API endpoints