The function below is intended to return the size of the base directory. Although additional files have been uploaded, it continues to display the same size. What am I missing?

function get_space_used() {
    $upload_dir = wp_upload_dir();
    $space_used = number_format( get_dirsize( $upload_dir['basedir'] ) / ( 1024 * 1024 ), 1 );
    return $space_used;
  • @kero you should post that as an answer
    – Tom J Nowell
    Mar 15, 2022 at 10:54
  • Just did it @TomJNowell
    – kero
    Mar 15, 2022 at 11:02

1 Answer 1


Under the hood, get_dirsize() uses recurse_dirsize() which uses a transient called dirsize_cache. Try clearing that transient and check again.

To do so, you can use one of the following methods:

Looking further through the source code, I think it should be possible to use recurse_dirsize() directly in your code and telling it not to cache like so:

$upload_dir = wp_upload_dir();
$size = recurse_dirsize($upload_dir['basedir'], null, null, []);

By passing an empty array [] as the fourth argument, it should circumvent the cache because the !isset($directory_cache) now returns a different result.

  • Thanks. Is there a way to disable the transient or alternatively is there another option that doesn't use a transient?
    – Motivated
    Mar 15, 2022 at 17:06
  • Did you check that the issue was resolved when deleting the cache? You could call the method directly and set $directory_cache to something so it doesn't use transients.
    – kero
    Mar 15, 2022 at 17:13
  • Yes, it does. Which method are you referring to? I've currently added delete_transient('dirsize_cache'); to the start of the function get_space_used(). I'm unsure if it's optimal and/or if it's the recommended way of achieving the outcome. I don't quite follow setting $directory_cache.
    – Motivated
    Mar 15, 2022 at 17:26
  • @Motivated I've added an example. Clearing the transient every time is probably not a good idea - it is there to cache the result to take load off the server. If the transient was wrong just this one time, it could be OK to use get_dirsize() regularly after all (without clearing transient first).
    – kero
    Mar 16, 2022 at 9:28
  • How does bypassing the cache differ to deleting the cache from a load perspective? Would it not be the same?
    – Motivated
    Mar 16, 2022 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.