Automate MinIO Image Cleanup with Spring Boot Scheduled Tasks

This article explains how to use MinIO as an image storage service and implement a Spring Boot scheduled task that periodically deletes outdated image folders, covering dependency setup, delete utilities, performance considerations, cron expressions, thread‑pool tuning, asynchronous execution, configuration extensions, and testing strategies.

Architect
Architect
Architect
Automate MinIO Image Cleanup with Spring Boot Scheduled Tasks

Overview

The project stores images in MinIO and needs a periodic cleanup to remove files older than a configurable date, reducing storage costs.

Dependencies

Only MinIO SDK and Spring Boot starter are required.

<dependency>
  <groupId>io.minio</groupId>
  <artifactId>minio</artifactId>
  <version>8.5.1</version>
</dependency>

Delete Utilities

The MinioUtil class provides methods to delete entire date folders and single prefixes.

public int deleteDateFoldersBefore(LocalDate endExclusive) { ... }
private int deleteSingleFolder(String prefix) { ... }

Performance Tips

Use listObjects pagination (default 1000 items).

Iterate removeObjects results to trigger HTTP calls.

Limit batch size with .maxKeys(batchSize) for massive object sets.

Scheduled Task Configuration

Enable scheduling with @EnableScheduling and define a cron expression.

@Scheduled(cron = "0 0 3 1 * ?")
public void minioClean() { ... }

Cron Expression Details

Field

Range

Special Characters

Second

0‑59

, - * /

Minute

0‑59

, - * /

Hour

0‑23

, - * /

Day

1‑31

, - * / ? L W

Month

1‑12 or JAN‑DEC

, - * /

Weekday

0‑7 (SUN‑SAT)

, - * / ? L #

Thread‑Pool Tuning

Spring’s ThreadPoolTaskScheduler can be customized to avoid task blocking.

ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(3);
scheduler.setThreadNamePrefix("minio-scheduler-");
...

Rejection Policies

AbortPolicy

– throws exception. CallerRunsPolicy – runs task in caller thread. DiscardPolicy – silently drops task. DiscardOldestPolicy – drops oldest queued task.

Asynchronous Execution

Define an async executor with @EnableAsync and annotate the cleanup method with @Async("taskExecutor") to prevent scheduler thread blockage.

@Async("taskExecutor")
@Scheduled(cron = "0 0 3 1 * ?")
public void minioClean() { ... }

Configuration Extensions

Externalize settings in application.yml (enabled, retain‑days, earliest‑date, cron) and bind them to a MinioCleanProperties class.

minio:
  clean:
    enabled: true
    retain-days: 5
    earliest-date: "2025/08/01"
    cron: "0/5 * * * * ?"

Testing

Use a JUnit test to invoke deleteDateFoldersBefore with a custom date, ensuring files exist before the run.

@SpringBootTest
class MinioTest {
  @Autowired MinioUtil minioUtil;
  @Test void testDelete() {
    int count = minioUtil.deleteDateFoldersBefore(LocalDate.of(2025,8,2));
    System.out.println(count);
  }
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring BootMinioScheduled TasksFile Cleanup
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.