-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticindexdelete.ts
40 lines (32 loc) · 1.43 KB
/
elasticindexdelete.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Client } from '@elastic/elasticsearch';
// Configuration
const ELASTICSEARCH_NODE: string = 'http://localhost:9200';
const GLOB_EXPRESSION: string = 'index-prefix-*';
async function deleteIndexes(globExpression: string): Promise<void> {
try {
const client = new Client({
node: ELASTICSEARCH_NODE,
});
// Fetch all index names
const response = await client.cat.indices({ format: 'json' });
const allIndexes: string[] = response.body.map((index: any) => index.index);
// Create the regex from the glob expression
const globToRegex = (glob: string) => new RegExp('^' + glob.split('*').map((part: string) => RegExp.escape(part)).join('.*') + '$');
const regex: RegExp = globToRegex(globExpression);
// Filter indexes based on glob expression
const indexesToDelete: string[] = allIndexes.filter((index: string) => regex.test(index));
// Delete the indexes
for (const index of indexesToDelete) {
await client.indices.delete({ index });
console.log(`Deleted index: ${index}`);
}
} catch (error) {
console.error('An error occurred:', error);
}
}
// This function escapes special characters in strings for regex
RegExp.escape = function(s: string) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
// Call the function with the desired glob expression
deleteIndexes(GLOB_EXPRESSION);