Implement Delete Task List

Now that we can create, retrieve, and update task lists, let's implement the ability to delete them. This will allow users to remove task lists they no longer need. Due to our cascade configuration, deleting a task list will also delete all tasks associated with it.

Updating the Service Interface

First, let's add the delete method to our TaskListService interface:

public interface TaskListService { List<TaskList> listTaskLists(); TaskList createTaskList(TaskList taskList); Optional<TaskList> getTaskList(UUID id); TaskList updateTaskList(UUID taskListId, TaskList taskList); void deleteTaskList(UUID taskListId); // Add this method }

Implementing the Service Method

Now let's implement the delete functionality in TaskListServiceImpl:

@Service public class TaskListServiceImpl implements TaskListService { private final TaskListRepository taskListRepository; public TaskListServiceImpl(TaskListRepository taskListRepository) { this.taskListRepository = taskListRepository; } @Override public void deleteTaskList(UUID taskListId) { taskListRepository.deleteById(taskListId); } }

This implementation is straightforward:

  1. Takes a UUID parameter representing the task list ID
  2. Delegates directly to the repository's deleteById method
  3. Returns void as deletion operations typically don't need to return anything

Note that we don't need to check if the task list exists before deleting it. Spring Data JPA's deleteById method handles non-existent entities gracefully.

Updating the Controller

Now let's add the delete endpoint to our TaskListController:

@RestController @RequestMapping(path = "/task-lists") public class TaskListController { private final TaskListService taskListService; private final TaskListMapper taskListMapper; public TaskListController(TaskListService taskListService, TaskListMapper taskListMapper) { this.taskListService = taskListService; this.taskListMapper = taskListMapper; } @DeleteMapping(path = "/{task_list_id}") public void deleteTaskList(@PathVariable("task_list_id") UUID taskListId) { taskListService.deleteTaskList(taskListId); } }

Let's break down the key components of this endpoint:

  1. URL Mapping:

    • @DeleteMapping maps this to HTTP DELETE requests
    • Path variable captures the task list ID from the URL
  2. Implementation:

    • Very simple - just delegates to the service layer
    • Returns void, which Spring will convert to a 200 OK response
    • No response body needed for DELETE operations

How It Works Together

When a DELETE request arrives at /task-lists/{id}:

  1. Spring extracts the ID from the URL path
  2. The controller calls taskListService.deleteTaskList()
  3. The service delegates to taskListRepository.deleteById()
  4. The repository:
    • Deletes the task list if it exists
    • Deletes all associated tasks (due to cascade configuration)
    • Does nothing if the task list doesn't exist
  5. Spring sends a 200 OK response with no body

For example, a DELETE request to /task-lists/123e4567-e89b-12d3-a456-426614174000 will:

  • Delete the task list with that ID if it exists
  • Delete all tasks in that list
  • Return a 200 OK status with no body

Cascade Delete Behavior

Remember that we configured cascade delete in our TaskList entity:

@OneToMany(mappedBy = "taskList", cascade = {CascadeType.REMOVE, CascadeType.PERSIST}) private List<Task> tasks;

This means when we delete a task list:

  • All tasks in the list are automatically deleted
  • No additional code is needed to maintain referential integrity
  • The database remains consistent

Summary

  • Added delete functionality to the TaskListService
  • Implemented a DELETE endpoint in the controller
  • Leveraged JPA cascade delete for associated tasks
  • Maintained consistent API behavior with other endpoints
  • Completed the CRUD operations for task lists
© 2026 Devtiro Ltd. All rights reserved