spring-projects/spring-ai · warning
Dropping existing TTL index, because TTL is different
Error message
Dropping existing TTL index, because TTL is different
What it means
MongoChatMemoryIndexCreatorAutoConfiguration.createOrUpdateTtlIndex (run from initIndicesAfterStartup) inspects existing indexes on the chat memory Conversation collection and drops any TTL index whose expireAfter value differs from the configured spring.ai.chat.memory.repository.mongodb ttl. This warning fires right before the drop; a new TTL index with the configured value is then created.
Source
Thrown at auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-mongodb/src/main/java/org/springframework/ai/model/chat/memory/repository/mongo/autoconfigure/MongoChatMemoryIndexCreatorAutoConfiguration.java:86
createOrUpdateTtlIndex();
}
private void createMainIndex() {
var indexOps = this.mongoTemplate.indexOps(Conversation.class);
var index = new Index().on("conversationId", Sort.Direction.ASC).on("timestamp", Sort.Direction.DESC);
// Use reflection to handle API differences across Spring Data MongoDB versions
createIndexSafely(indexOps, index);
}
private void createOrUpdateTtlIndex() {
if (!this.mongoChatMemoryProperties.getTtl().isZero()) {
var indexOps = this.mongoTemplate.indexOps(Conversation.class);
// Check for existing TTL index
indexOps.getIndexInfo().forEach(idx -> {
if (idx.getExpireAfter().isPresent()
&& !idx.getExpireAfter().get().equals(this.mongoChatMemoryProperties.getTtl())) {
logger.warn("Dropping existing TTL index, because TTL is different");
indexOps.dropIndex(idx.getName());
}
});
// Use reflection to handle API differences across Spring Data MongoDB
// versions
createIndexSafely(indexOps,
new Index().on("timestamp", Sort.Direction.ASC).expire(this.mongoChatMemoryProperties.getTtl()));
}
}
/**
* Creates an index using reflection to handle API changes across different Spring
* Data MongoDB versions:
* <ul>
* <li>Spring Data MongoDB 4.2.x - 4.4.x: only {@code ensureIndex(IndexDefinition)} is
* available.</li>
* <li>Spring Data MongoDB 4.5.x+: {@code createIndex(IndexDefinition)} is the new
* API, {@code ensureIndex} is deprecated.</li>View on GitHub (pinned to 98a7beda4f)
Solutions
- Align the application's configured ttl with the existing index value if the old retention is intended.
- Accept the drop/recreate if changing retention is intentional (note: changing TTL applies to all documents).
- Dedicate separate collections/databases per service so differing TTLs do not fight.
Example fix
// before (application.yml) spring.ai.chat.memory.repository.mongodb.ttl: 1d // after (match existing index TTL of 7d) spring.ai.chat.memory.repository.mongodb.ttl: 7d
Defensive patterns
Strategy: validation
Validate before calling
Duration configured = mongoChatMemoryProperties.getTtl();
boolean mismatch = mongoTemplate.indexOps(Conversation.class).getIndexInfo().stream()
.flatMap(i -> i.getExpireAfter().stream())
.anyMatch(exp -> !exp.equals(configured));
if (mismatch) {
// index will be dropped and recreated; confirm retention change is intentional
} Prevention
- Check existing TTL indexes (db.conversation.getIndexes()) before changing the ttl property.
- Avoid multiple services sharing a collection with different TTLs.
- Treat TTL changes as schema migrations with review.
When it happens
Trigger: Startup with a non-zero configured TTL that does not match the TTL of an existing index on the Conversation collection, e.g. changing spring.ai.chat.memory.repository.mongodb.ttl from 1d to 7d, or an index created by an earlier version/another service with a different expiry.
Common situations: Tuning chat-memory retention in an existing deployment; two services sharing one MongoDB database with different TTL settings, causing index drop/recreate flapping on each startup; migration from a schema created manually via mongosh.
Related errors
- Unsupported message type:
- MongoChatMemoryRepository does not support tool call message
- Unsupported message type: + conversation.message().type()
- 'spring.ai.chat.client.tool-search-advisor.tool-index-type=v
- Neither createIndex() nor ensureIndex() method found on Inde
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/2df02900995090d7.
Report an issue: GitHub.