diff --git a/model/instance/lifecycle/destroy.go b/model/instance/lifecycle/destroy.go index 6d1ee6fe433..f9b47e71c01 100644 --- a/model/instance/lifecycle/destroy.go +++ b/model/instance/lifecycle/destroy.go @@ -10,6 +10,7 @@ import ( "github.com/cozy/cozy-stack/model/app" "github.com/cozy/cozy-stack/model/instance" job "github.com/cozy/cozy-stack/model/job" + "github.com/cozy/cozy-stack/model/rag" "github.com/cozy/cozy-stack/pkg/config/config" "github.com/cozy/cozy-stack/pkg/consts" "github.com/cozy/cozy-stack/pkg/couchdb" @@ -76,6 +77,10 @@ func Destroy(domain string) error { return err } + if err := rag.CleanInstance(inst); err != nil { + return err + } + // Reload the instance, it can have been updated in CouchDB if the instance // had at least one account and was not up-to-date for its indexes/views. inst, err = instance.Get(domain) diff --git a/model/rag/index.go b/model/rag/index.go index 3962e90932f..9d620c19224 100644 --- a/model/rag/index.go +++ b/model/rag/index.go @@ -252,3 +252,29 @@ func pushJob(inst *instance.Instance, doctype string) error { }) return err } + +func CleanInstance(inst *instance.Instance) error { + ragServer := inst.RAGServer() + if ragServer.URL == "" { + return nil + } + u, err := url.Parse(ragServer.URL) + if err != nil { + return err + } + u.Path = fmt.Sprintf("/instances/%s", inst.Domain) + req, err := http.NewRequest(http.MethodDelete, u.String(), nil) + if err != nil { + return err + } + req.Header.Add(echo.HeaderAuthorization, "Bearer "+ragServer.APIKey) + res, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer res.Body.Close() + if res.StatusCode >= 500 { + return fmt.Errorf("DELETE status code: %d", res.StatusCode) + } + return nil +}