Skip to content

Commit

Permalink
🔨 add command for deleting Algolia index (#3777)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marigold authored Jul 10, 2024
1 parent 8c310f3 commit e16e534
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ reindex: itsJustJavascript
node --enable-source-maps itsJustJavascript/baker/algolia/indexChartsToAlgolia.js
node --enable-source-maps itsJustJavascript/baker/algolia/indexExplorerViewsToAlgolia.js

delete-algolia-index: itsJustJavascript
@echo '==> Deleting Algolia index'
node --enable-source-maps itsJustJavascript/baker/algolia/deleteAlgoliaIndex.js

bench.search: itsJustJavascript
@echo '==> Running search benchmarks'
@node --enable-source-maps itsJustJavascript/site/search/evaluateSearch.js
Expand Down
46 changes: 46 additions & 0 deletions baker/algolia/deleteAlgoliaIndex.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ALGOLIA_INDEXING } from "../../settings/serverSettings.js"
import { ALGOLIA_INDEX_PREFIX } from "../../settings/clientSettings.js"
import { getAlgoliaClient } from "./configureAlgolia.js"
import { SearchIndexName } from "../../site/search/searchTypes.js"
import { getIndexName } from "../../site/search/searchClient.js"

const deleteAlgoliaIndex = async () => {
if (!ALGOLIA_INDEXING) return

if (ALGOLIA_INDEX_PREFIX === "") {
console.error(
"ALGOLIA_INDEX_PREFIX is not set, refusing to delete production indexes"
)
return
}

const client = getAlgoliaClient()
if (!client) {
console.error(`Failed to remove index (Algolia client not initialized)`)
return
}

for (const suffix of [
SearchIndexName.Pages,
SearchIndexName.Charts,
SearchIndexName.ExplorerViews,
]) {
const indexName = getIndexName(suffix)
const index = client.initIndex(indexName)
try {
await index.delete()
console.log(`Index '${indexName}' removed successfully`)
} catch (error) {
console.error(`Error removing index '${indexName}':`, error)
}
}

process.exit(0)
}

process.on("unhandledRejection", (e) => {
console.error(e)
process.exit(1)
})

void deleteAlgoliaIndex()

0 comments on commit e16e534

Please sign in to comment.