How to Use deleteById in Spring Boot: Simple Guide
In Spring Boot, use
deleteById(ID id) method from a repository interface that extends JpaRepository or CrudRepository to delete an entity by its ID. Simply call this method with the entity's ID, and Spring will remove the record from the database.Syntax
The deleteById method is declared in Spring Data repository interfaces like JpaRepository or CrudRepository. It takes one parameter:
id: The unique identifier of the entity to delete.
When called, it deletes the entity with the given ID from the database.
java
void deleteById(ID id);Example
This example shows a Spring Boot repository deleting a user by their ID. The UserRepository extends JpaRepository, which provides deleteById. The UserService calls deleteById to remove a user.
java
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import jakarta.persistence.Entity; import jakarta.persistence.Id; @Entity class User { @Id private Long id; private String name; // Constructors, getters, setters public User() {} public User(Long id, String name) { this.id = id; this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @Repository interface UserRepository extends JpaRepository<User, Long> {} @Service class UserService { @Autowired private UserRepository userRepository; public void deleteUserById(Long id) { userRepository.deleteById(id); } } // Usage example (e.g., in a controller or test): // userService.deleteUserById(1L);
Output
User with ID 1 is deleted from the database if it exists.
Common Pitfalls
Common mistakes when using deleteById include:
- Calling
deleteByIdwith an ID that does not exist causesEmptyResultDataAccessException. - Not handling exceptions when the ID is missing.
- Assuming
deleteByIdreturns a value; it returnsvoid.
To avoid errors, check if the entity exists before deleting or catch exceptions.
java
try { userRepository.deleteById(nonExistingId); } catch (org.springframework.dao.EmptyResultDataAccessException e) { // Handle case when ID does not exist } // Or check existence first if (userRepository.existsById(id)) { userRepository.deleteById(id); }
Quick Reference
deleteById Cheat Sheet:
| Method | Description |
|---|---|
| deleteById(ID id) | Deletes entity by ID, throws exception if ID not found |
| existsById(ID id) | Checks if entity exists by ID |
| Method | Description |
|---|---|
| deleteById(ID id) | Deletes entity by ID, throws exception if ID not found |
| existsById(ID id) | Checks if entity exists by ID |
Key Takeaways
Use deleteById(ID id) from JpaRepository or CrudRepository to delete entities by ID.
deleteById throws an exception if the ID does not exist; handle or check existence first.
deleteById returns void and performs deletion directly in the database.
Always ensure the ID is valid and exists to avoid runtime exceptions.
Use existsById(ID id) to check presence before deleting if needed.