One common practice is to delete database records which are older than some threshold value. For this purpose, the database table should have a column with a timestamp representing insert or update time. When executing the scheduled task there a command is performed (pseudocode, assuming an 'update' timestamp):
DELETE FROM TABLE_NAME WHERE LAST_UPDATED < (CURRENT_TIME - THRESHOLD)
Or there is a select query performed to retrieve all these records and handle their deletion one by one including logging and maybe some conditional logic.
To achieve this functionality java SE provides ScheduledExecutorService which can be used to execute delayed tasks:
// initialize scheduler
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
// schedule repeating task
ScheduledFuture future = scheduler.scheduleAtFixedRate(
() -> {
System.out.println("Excuting task...");
},
60,
60,
TimeUnit.SECONDS
);
The future object can be used to cancel task:
future.cancel(true);
scheduler.shutdown();
But when using enterprise solutions, there are normally libraries and frameworks, providing functionality for executing scheduled tasks: quartz scheduler, spring framework task scheduling or EJB timer services.