Filters whose porosity decreases with depth are often more efficient at removing solute from a fluid than filters with a uniform porosity. We investigate this phenomenon via an extension of homogenization theory that accounts for a macroscale variation in microstructure. In the first stage of the paper, we homogenize the problems of flow through a filter with a near-periodic microstructure and of solute transport owing to advection, diffusion and filter adsorption. In the second stage, we use the computationally efficient homogenized equations to investigate and quantify why porosity gradients can improve filter efficiency. We find that a porosity gradient has a much larger effect on the uniformity of adsorption than it does on the total adsorption. This allows us to understand how a decreasing porosity can lead to a greater filter efficiency, by lowering the risk of localized blocking while maintaining the rate of total contaminant removal.