# Email Alert When Disk Space Runs Low

> Checks free disk space on the server twice a day and emails the admin when it drops below your threshold, before uploads, backups and the database start failing.

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, monitoring, server
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-low-disk-space-alert/

## Code

```php
$wpide_min_free_gb = 2;

add_action( 'init', function () {
    if ( ! wp_next_scheduled( 'wpide_disk_space_check' ) ) {
        wp_schedule_event( time() + HOUR_IN_SECONDS, 'twicedaily', 'wpide_disk_space_check' );
    }
} );

add_action( 'wpide_disk_space_check', function () use ( $wpide_min_free_gb ) {
    if ( ! function_exists( 'disk_free_space' ) || get_transient( 'wpide_disk_alert_sent' ) ) {
        return;
    }

    $free = @disk_free_space( ABSPATH ); // phpcs:ignore WordPress.PHP.NoSilencedErrors
    if ( false === $free || $free >= $wpide_min_free_gb * GB_IN_BYTES ) {
        return;
    }

    set_transient( 'wpide_disk_alert_sent', 1, DAY_IN_SECONDS );
    wp_mail(
        get_option( 'admin_email' ),
        sprintf( '[%s] Low disk space: %s free', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), size_format( $free ) ),
        sprintf( "The server hosting %s has only %s of free disk space left (alert threshold: %d GB).\n\nClear old backups, logs or unused media, or ask your host for more space.", home_url(), size_format( $free ), $wpide_min_free_gb )
    );
} );
```

## Notes

Does nothing on hosts that disable disk_free_space(), and repeats the alert at most once a day while space stays low.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Email Alert When Disk Space Runs Low" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
