Cleaning up old backups | laravel-backup | Spatie

 SPATIE

  Laravel Backup
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-backup](https://spatie.be/docs/laravel-backup/v3)  Cleaning-up-old-backups  Cleaning up old backups

 Version   v10   v9   v8   v7   v6   v5   v4   v3

 Other versions for crawler [v10](https://spatie.be/docs/laravel-backup/v10) [v9](https://spatie.be/docs/laravel-backup/v9) [v8](https://spatie.be/docs/laravel-backup/v8) [v7](https://spatie.be/docs/laravel-backup/v7) [v6](https://spatie.be/docs/laravel-backup/v6) [v5](https://spatie.be/docs/laravel-backup/v5) [v4](https://spatie.be/docs/laravel-backup/v4) [v3](https://spatie.be/docs/laravel-backup/v3)

  Cleaning up old backups
- [ Introduction ](https://spatie.be/docs/laravel-backup/v3/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-backup/v3/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-backup/v3/requirements)
- [ High level overview ](https://spatie.be/docs/laravel-backup/v3/high-level-overview)
- [ Installation and setup ](https://spatie.be/docs/laravel-backup/v3/installation-and-setup)
- [ Questions &amp; issues ](https://spatie.be/docs/laravel-backup/v3/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-backup/v3/changelog)
- [ About us ](https://spatie.be/docs/laravel-backup/v3/about-us)

Taking Backups
--------------

- [ Taking backups ](https://spatie.be/docs/laravel-backup/v3/taking-backups/overview)
- [ Events ](https://spatie.be/docs/laravel-backup/v3/taking-backups/events)

Cleaning up old backups
-----------------------

- [ Cleaning up old backups ](https://spatie.be/docs/laravel-backup/v3/cleaning-up-old-backups/overview)
- [ Events ](https://spatie.be/docs/laravel-backup/v3/cleaning-up-old-backups/events)

Monitoring the health of all backups
------------------------------------

- [ Monitoring the health of all backups ](https://spatie.be/docs/laravel-backup/v3/monitoring-the-health-of-all-backups/overview)
- [ Events ](https://spatie.be/docs/laravel-backup/v3/monitoring-the-health-of-all-backups/events)

Sending notifications
---------------------

- [ Sending notifications ](https://spatie.be/docs/laravel-backup/v3/sending-notifications/overview)
- [ Creating a custom sender ](https://spatie.be/docs/laravel-backup/v3/sending-notifications/creating-a-custom-sender)

Advanced Usage
--------------

- [ Adding Extra Files to the zip ](https://spatie.be/docs/laravel-backup/v3/advanced-usage/adding-extra-files-to-the-zip)
- [ Backing up a non-laravel application ](https://spatie.be/docs/laravel-backup/v3/advanced-usage/backing-up-a-non-laravel-application)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-backup                                                                                                                                                                                                                                    `

Cleaning up old backups
=======================

###  On this page

1. [ Determining which backups should be deleted ](#content-determining-which-backups-should-be-deleted)
2. [ Creating your own strategy ](#content-creating-your-own-strategy)
3. [ Getting notified when a cleanup goes wrong ](#content-getting-notified-when-a-cleanup-goes-wrong)

Over time the amount of backups and the storage needed to keep them will grow. At some point you are going to want to clean up old backups.

You can clean up your backups by running:

```
php artisan backup:clean
```

We'll tell you right off the bat that the package by default will never delete the youngest backup regardless it's size or age.

Determining which backups should be deleted
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This is the portion of the configuration that will determine which backups should be deleted.

```
//config/laravel-backup.php

    'cleanup' => [
        /*
         * The strategy that will be used to cleanup old backups.
         * The youngest backup wil never be deleted.
         */
        'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class,

        'defaultStrategy' => [

            /*
             * The amount of days that all daily backups must be kept.
             */
            'keepAllBackupsForDays' => 7,

            /*
             * The amount of days that daily backups must be kept.
             */
            'keepDailyBackupsForDays' => 16,

            /*
             * The amount of weeks of which one weekly backup must be kept.
             */
            'keepWeeklyBackupsForWeeks' => 8,

            /*
             * The amount of months of which one monthly backup must be kept.
             */
            'keepMonthlyBackupsForMonths' => 4,

            /*
             * The amount of years of which one yearly backup must be kept
             */
            'keepYearlyBackupsForYears' => 2,

            /*
             * After clean up the backups remove the oldest backup until
             * this amount of megabytes is reached.
             */
            'deleteOldestBackupsWhenUsingMoreMegabytesThan' => 5000
        ]
    ],
```

This package provides an opinionated method to determine which old backups should be deleted. We call this the `DefaultStrategy`. This is how it works:

- Rule #1: it will never delete the youngest backup regardless of it's size or age
- Rule #2: it will keep all backups for the amount of days specified in `keepAllBackupsForDays`
- Rule #3: it'll only keep daily backups for the amount of days specified in `keepDailyBackupsForDays` for all backups older than those that rule #2 takes care of
- Rule #4: it'll only keep weekly backups for the amount of months specified in `keepMonthlyBackupsForMonths` for all backups older than those that rule #3 takes care of
- Rule #5: it'll only keep yearly backups for the amount of years specified in `keepYearlyBackupsForYears` for all backups older than those that rule #4 takes care of
- Rule #6: it will start deleting old backups until the used storage is lower than the number specified in `deleteOldestBackupsWhenUsingMoreMegabytesThan`.

Of course the numbers used in the default configuration can be adjusted to your own liking.

Creating your own strategy
--------------------------------------------------------------------------------------------------------------------------------------

If you are not happy with the `DefaultStrategy`, you can create your own custom strategy. You can do so by extending the abstract class `Spatie\Backup\Tasks\Cleanup\CleanupStrategy`. You only need to implement this method:

```
use Spatie\Backup\BackupDestination\BackupCollection;

public function deleteOldBackups(BackupCollection $backupCollection)
```

The `BackupCollection` class is extended of `Illuminate\Support\Collection` and contains `Spatie\Backup\BackupDestination\Backup`-objects sorted by age. The youngest backup is the first one in the collection.

Using the collection, you can easily manually delete the oldest backup:

```
// Retrieve an instance of `Spatie\Backup\BackupDestination\Backup`
$backup = $backups->oldestBackup();

// Bye bye backup
$backup->delete();
```

Don't forget to specify the full classname of your custom strategy in the `cleanup.strategy` key in the `laravel-backup` config file.

Getting notified when a cleanup goes wrong
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can receive a notification when a cleanup goes wrong. Read the section on [notifications](/laravel-backup/v3/sending-notifications/overview) to know more.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
