@@ -174,16 +174,17 @@ func cleanup(log *logger.Logger, topDirPath string, removeMarker, keepLogs bool,
174174
175175 dirPrefix := fmt .Sprintf ("%s-" , AgentName )
176176
177+ var cumulativeError error
177178 relativeHomePaths := make ([]string , len (versionedHomesToKeep ))
178179 for i , h := range versionedHomesToKeep {
179- relHomePath , err := filepath .Rel ("data" , h )
180+ relHomePath , err := filepath .Rel (dataDirPath , filepath . Join ( topDirPath , h ) )
180181 if err != nil {
181- return fmt .Errorf ("extracting elastic-agent path relative to data directory from %s: %w" , h , err )
182+ cumulativeError = goerrors .Join (cumulativeError , fmt .Errorf ("extracting elastic-agent path relative to data directory from %s: %w" , h , err ))
183+ continue
182184 }
183185 relativeHomePaths [i ] = relHomePath
184186 }
185187
186- var errs []error
187188 for _ , dir := range subdirs {
188189 if slices .Contains (relativeHomePaths , dir ) {
189190 continue
@@ -200,11 +201,11 @@ func cleanup(log *logger.Logger, topDirPath string, removeMarker, keepLogs bool,
200201 ignoredDirs = append (ignoredDirs , "logs" )
201202 }
202203 if cleanupErr := install .RemoveBut (hashedDir , true , ignoredDirs ... ); cleanupErr != nil {
203- errs = append ( errs , cleanupErr )
204+ cumulativeError = goerrors . Join ( cumulativeError , cleanupErr )
204205 }
205206 }
206207
207- return goerrors . Join ( errs ... )
208+ return cumulativeError
208209}
209210
210211// InvokeWatcher invokes an agent instance using watcher argument for watching behavior of
0 commit comments