@Override
  public void cleanUpStagingDirectory(@Nonnull String targetPath, @Nonnull String stagingDirectoryName,@Nonnull String storeId)
      throws IOException {
    Objects.requireNonNull(targetPath, "targetPath must not be null");
    Objects.requireNonNull(stagingDirectoryName, "stagingDirectoryName must not be null");
    Objects.requireNonNull(storeId, "storeId must not be null");

    File rootDir = new File(targetPath);
    if (!rootDir.exists() || !rootDir.isDirectory()) {
      throw new IOException("Invalid target path: " + targetPath);
    }

    File stagingDir = new File(rootDir, stagingDirectoryName);
    if (!stagingDir.exists() || !stagingDir.isDirectory()) {
      logger.info("Staging Directory Does not Exist: {}", stagingDir.getAbsolutePath());
      return;
    }

    // 1. Find prefixes from *_index and *_log files in staging directory
    Set<String> prefixes = new HashSet<>();
    File[] stagingFiles = stagingDir.listFiles();
    if (stagingFiles != null) {
      for (File file : stagingFiles) {
        String name = file.getName();
        if (name.endsWith(INDEX_SUFFIX) || name.endsWith(LOG_SUFFIX)) {
          int lastUnderscore = name.lastIndexOf('_');
          if(lastUnderscore != -1){
            if(name.endsWith(LOG_SUFFIX)){
              // If the file ends with _log, we add the prefix without the last underscore
              prefixes.add(name.substring(0, lastUnderscore));
            } else {
              // If the file ends with _index, we add the prefix without the last underscore
              int secondLastUnderscore = name.lastIndexOf('_', lastUnderscore - 1);
              prefixes.add(name.substring(0, secondLastUnderscore));
            }
          }
        }
      }

      // 2. Delete all files in staging directory
      for (File file : stagingFiles) {
        if(!file.isDirectory() && file.getName().endsWith(LOG_SUFFIX)) {
          cleanLogFile(file.getAbsolutePath(), storeId);
        } else {
          deleteRecursively(file);
        }
      }
    }
    if (!stagingDir.delete()) {
      throw new IOException("Failed to delete staging directory: " + stagingDir.getAbsolutePath());
    }

    // 3. Delete files in RootDir with matching prefixes
    File[] targetFiles = rootDir.listFiles();
    if (targetFiles != null) {
      for (File file : targetFiles) {
        String name = file.getName();
        for (String prefix : prefixes) {
          if (name.startsWith(prefix)) {
            if(name.endsWith(LOG_SUFFIX)) {
              try {
                cleanLogFile(file.getAbsolutePath(), storeId);
              } catch (IOException e) {
                logger.error("Failed to clean log file: {}", file.getAbsolutePath(), e);
                throw new FileStoreException("Error while cleaning log file: " + file.getAbsolutePath(),
                    FileStoreErrorCode.FileStoreFileFailedCleanUp);
              }
            } else {
              file.delete();
            }
          }
        }
      }
    }
  }
Cleans up the staging directory by deleting all files and directories within it.
It also deletes any files in the root directory that match the prefixes of the staging files.

@param targetPath The path to the root directory where the staging directory is located
@param stagingDirectoryName The name of the staging directory to clean up
@param storeId The store ID for which the cleanup is being performed
@throws IOException if an I/O error occurs during cleanup
ambry-store/src/main/java/com/github/ambry/store/FileStore.java