You could use a context manager to clean it up automatically:
from contextlib import contextmanager
import rasterio
from rasterio import MemoryFile
@contextmanager
def mem_raster(data, **profile):
with MemoryFile() as memfile:
with memfile.open(**profile) as dataset_writer:
dataset_writer.write(data)
with memfile.open() as dataset_reader:
yield dataset_reader
#setup, get data from somewhere, copy or create profile etc...
with mem_raster(data, **profile) as ds:
do_something_with(ds)
# the memfile is cleaned up after exiting the with context