Skip to content

Commit 6f4f4ba

Browse files
committed
Cleanup all orphaned indices without alias
1 parent 3574909 commit 6f4f4ba

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

oscar_elasticsearch/search/indexing/indexer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ def finish(self):
8383
# No indices yet, make alias from original name to alias name
8484
es.indices.put_alias(name=self.name, index=self.alias_name)
8585

86+
# Get all indices associated with the alias
87+
aliased_indices = list(
88+
es.indices.get_alias(name=self.name, ignore_unavailable=True).keys()
89+
)
90+
91+
# Find all indices matching the pattern but without an alias
92+
all_indices = list(
93+
es.indices.get(index=f"{self.name}_*", ignore_unavailable=True).keys()
94+
)
95+
unaliased_indices = set(all_indices) - set(aliased_indices)
96+
97+
for index in unaliased_indices:
98+
self.delete(index)
99+
86100
def create(self, name):
87101
return es.indices.create(
88102
index=name, body={"settings": self.settings, "mappings": self.mappings}

0 commit comments

Comments
 (0)