public void deleteDataRegion(DataRegionId regionId) {
    if (!dataRegionMap.containsKey(regionId) || deletingDataRegionMap.containsKey(regionId)) {
      return;
    }
    DataRegion region =
        deletingDataRegionMap.computeIfAbsent(regionId, k -> dataRegionMap.remove(regionId));
    if (region != null) {
      region.markDeleted();
      try {
        region.abortCompaction();
        region.syncDeleteDataFiles();
        region.deleteFolder(systemDir);
        region.deleteDALFolderAndClose();
        PipeDataNodeAgent.receiver().pipeConsensus().releaseReceiverResource(regionId);
        switch (CONFIG.getDataRegionConsensusProtocolClass()) {
          case ConsensusFactory.IOT_CONSENSUS:
          case ConsensusFactory.IOT_CONSENSUS_V2:
            // delete wal
            WALManager.getInstance()
                .deleteWALNode(
                    region.getDatabaseName() + FILE_NAME_SEPARATOR + region.getDataRegionId());
            // delete snapshot
            for (String dataDir : CONFIG.getLocalDataDirs()) {
              File regionSnapshotDir =
                  new File(
                      dataDir + File.separator + IoTDBConstant.SNAPSHOT_FOLDER_NAME,
                      region.getDatabaseName() + FILE_NAME_SEPARATOR + regionId.getId());
              if (regionSnapshotDir.exists()) {
                try {
                  FileUtils.deleteDirectory(regionSnapshotDir);
                } catch (IOException e) {
                  LOGGER.error("Failed to delete snapshot dir {}", regionSnapshotDir, e);
                }
              }
            }
            break;
          case ConsensusFactory.SIMPLE_CONSENSUS:
            // delete region information in wal and may delete wal
            WALManager.getInstance()
                .deleteRegionAndMayDeleteWALNode(
                    region.getDatabaseName(), region.getDataRegionId());
            break;
          case ConsensusFactory.RATIS_CONSENSUS:
          default:
            break;
        }
        WRITING_METRICS.removeDataRegionMemoryCostMetrics(regionId);
        WRITING_METRICS.removeFlushingMemTableStatusMetrics(regionId);
        WRITING_METRICS.removeActiveMemtableCounterMetrics(regionId);
        FileMetrics.getInstance().deleteRegion(region.getDatabaseName(), region.getDataRegionId());
      } catch (Exception e) {
        LOGGER.error(
            "Error occurs when deleting data region {}-{}",
            region.getDatabaseName(),
            region.getDataRegionId(),
            e);
      } finally {
        deletingDataRegionMap.remove(regionId);
      }
    }
  }
Add a listener to listen file close events. Notice that this addition only applies to
TsFileProcessors created afterward.
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/StorageEngine.java