VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/spring-data-jpa-delete-records-from-mysql/

⇱ Spring Data JPA - Delete Records From MySQL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Data JPA - Delete Records From MySQL

Last Updated : 23 Apr, 2026

Spring Data JPA makes deleting records from a MySQL database simple by providing built-in repository methods like deleteById() and delete(). It eliminates the need for manual SQL queries and integrates seamlessly with Spring Boot for efficient data management.

  • deleteById() is a predefined method of JpaRepository
  • No need to write custom SQL queries
  • Can be combined with existsById() for safe deletion

Real-world example: In an e-commerce application, when a user cancels their account, then we can use Spring Data JPA to delete the user record from the database. Similarly, an admin panel may allow administrators to remove inactive users or outdated products using repository delete methods without writing custom SQL.

Step By Step Implementation to Delete a record

Follow these steps to create, configure, and run a Spring Boot application that deletes records from a MySQL database using the deleteById() method in Spring Data JPA.

Step 1: Create a Spring Boot Project

1. Go to Spring Initializr
2. Fill the following details:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: 3.x.x (Latest stable version)
  • Group: com.example
  • Artifact: SpringBootApp
  • Name: SpringBootApp
  • PackageName: com.example
  • Packaging: Jar
  • Java Version: 17

3. Add the following dependencies:

  • Spring Data JPA
  • Spring Web
  • MYSQL Driver

4. Click on Generate to download the project as a ZIP file.

👁 CreatingProject

Step 2: Open Project in IDE

  • Extract the zip file
  • Now open a suitable IDE and then go to File->New->Project from existing sources->Springbootapp and select pom.xml. Click on import changes on prompt and wait for the project to sync.
👁 Image

Note: Ensure the same JDK version is selected while importing the project. Also, configure the database connection before running the application.

Step 3: Add Required Dependencies

The pom.xml file contains the project dependencies and configuration.

Step 4: Configure Database in (application.properties)

Add the following properties of database before executing the program in src/main/resources/application.properties file

spring.datasource.url=jdbc:mysql://localhost:3306/userdb
spring.datasource.username=root # replace with your MySQL username
spring.datasource.password=yourpassword # replace with your MySQL password
spring.jpa.hibernate.ddl-auto=update

Project Directory Structure:

👁 ProjectStructure

Step 5: Create the Entity Class

Create a User entity class that represents a table in the MySQL database.

Step 6: Create the Repository Interface

Create a repository interface that extends JpaRepository to perform CRUD operations.

Step 7: Implement the Main Application Class

The main application class implements CommandLineRunner to execute code at startup. Here, we insert a record and then delete it using the deleteById() method.

Step 8: Run the Application

Ensure MySQL is running and the user database exists. Run the application using the following command

mvn spring-boot:run

Output:

👁 Run

Step 9: Test The API

Use Postman or browser

  • If record exists -> "Record deleted successfully."
  • If not -> "Record not found."

DELETE http://localhost:8080/users/1

Database Output:

👁 DatabaseOutput
Comment
Article Tags:

Explore