Deleting Expired Drafts

A draft expires when it is older than the period set in the jmix.uirecovery.drafts.retention property. The add-on deletes an expired draft when it finds the draft, that is when the user opens the view of the draft or the Unsaved drafts view. Drafts of the views that users never open again stay in the database. To delete them, set up a regular job.

The job calls the deleteExpired() method of the ViewDraftService bean. The method deletes expired drafts of all users and doesn’t require an authenticated user, so you can call it from any scheduled task.

For example, the following bean uses the Spring @Scheduled annotation:

@Component
public class DraftsCleanupJob {

    private static final Logger log = LoggerFactory.getLogger(DraftsCleanupJob.class);

    private final ViewDraftService viewDraftService;

    public DraftsCleanupJob(ViewDraftService viewDraftService) {
        this.viewDraftService = viewDraftService;
    }

    @Scheduled(cron = "0 0 3 * * *") (1)
    public void deleteExpiredDrafts() {
        int count = viewDraftService.deleteExpired(); (2)
        log.info("Deleted {} expired drafts", count);
    }
}
1 Runs the method every day at 3 a.m.
2 Deletes the expired drafts and returns their number.

Scheduling with @Scheduled must be enabled in a configuration class:

@Configuration
@EnableScheduling
public class SchedulingConfiguration {
}

You can also call the method from a job of the Quartz add-on.