{"record":{"id":"997880036774e86b","repo":"gofr-dev/gofr","slug":"w-deleting-index-w","errorCode":null,"errorMessage":"%w: deleting index: %w","messagePattern":"%w: deleting index: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":165,"sourceCode":"\treturn nil\n}\n\nfunc (c *Client) DeleteIndex(ctx context.Context, index string) error {\n\tif strings.TrimSpace(index) == \"\" {\n\t\treturn errEmptyIndex\n\t}\n\n\tstart := time.Now()\n\n\ttracedCtx, span := c.addTrace(ctx, \"delete-index\", []string{index}, \"\")\n\n\treq := esapi.IndicesDeleteRequest{\n\t\tIndex: []string{index},\n\t}\n\n\tres, err := req.Do(tracedCtx, c.client)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"%w: deleting index: %w\", errOperation, err)\n\t}\n\tdefer res.Body.Close()\n\n\tif res.IsError() {\n\t\treturn fmt.Errorf(\"%w: %s\", errResponse, res.String())\n\t}\n\n\tc.sendOperationStats(start, fmt.Sprintf(\"DELETE INDEX %s\", index),\n\t\t[]string{index}, \"\", nil, span)\n\n\treturn nil\n}\n\n// Search executes a query against one or more indices.\n// Returns the entire response JSON as a map.\nfunc (c *Client) Search(ctx context.Context, indices []string, query map[string]any) (map[string]any, error) {\n\tif len(indices) == 0 {\n\t\treturn nil, errEmptyIndex","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L147-L183","documentation":"This is errOperation wrapped around the transport error from esapi.IndicesDeleteRequest.Do in DeleteIndex. The delete request never reached the cluster or failed at the HTTP layer: unreachable host, DNS/TLS problems, connection reset, or an expired/cancelled context.","triggerScenarios":"Calling DeleteIndex when the ES host is unreachable or down, network/firewall drops the connection, Connect() failed earlier leaving a broken client, or the provided context times out mid-request.","commonSituations":"Developers hit this during teardown/cleanup jobs after the cluster was stopped, in CI where the ES service container isn't ready yet, or with misconfigured Addresses (wrong host/port).","solutions":["Confirm the cluster is reachable: curl the ES endpoint from the app host.","Check Config.Addresses and port (9200 for HTTP).","Verify Connect()/HealthCheck succeeded before deleting.","Give the context a sufficient deadline; retry transient network errors with backoff."],"exampleFix":"// before\nerr := client.DeleteIndex(ctx, \"orders-2026\") // cluster not up yet in CI\n// after\nwaitForElasticsearch(t) // poll HealthCheck until UP\nerr := client.DeleteIndex(ctx, \"orders-2026\")","handlingStrategy":"retry","validationCode":"ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\ndefer cancel()\nif _, err := client.HealthCheck(ctx); err != nil {\n    return fmt.Errorf(\"elasticsearch unreachable, skipping delete: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"var lastErr error\nfor attempt := 0; attempt < 3; attempt++ {\n    err := client.DeleteIndex(ctx, index)\n    if err == nil {\n        return nil\n    }\n    lastErr = err\n    if strings.Contains(err.Error(), \"deleting index:\") && isTransient(err) {\n        time.Sleep(time.Duration(attempt+1) * time.Second)\n        continue\n    }\n    break\n}\nreturn lastErr","preventionTips":["Gate index deletion behind explicit checks that the cluster is reachable.","In CI, wait for the ES service to be healthy before running cleanup.","Use generous context deadlines; deleting large indices can take time.","Log and inspect the inner transport error to distinguish refused/DNS/timeout."],"tags":["elasticsearch","network","connection","delete-index"],"backgroundTag":"connection-refused","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}