Cleaning up old backups | laravel-backup-rust | Spatie       

 SPATIE  

laravel-backup-rust
===================

spatie.be/open-source

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

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-backup-rust/v1) 

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

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

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

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

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

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

- [ Monitoring the health of all backups ](https://spatie.be/docs/laravel-backup-rust/v1/monitoring-the-health-of-all-backups/overview)
- [ Creating your custom health check ](https://spatie.be/docs/laravel-backup-rust/v1/monitoring-the-health-of-all-backups/creating-your-custom-health-check)
- [ Events ](https://spatie.be/docs/laravel-backup-rust/v1/monitoring-the-health-of-all-backups/events)

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

- [ Sending notifications ](https://spatie.be/docs/laravel-backup-rust/v1/sending-notifications/overview)
- [ Adding extra notification channels ](https://spatie.be/docs/laravel-backup-rust/v1/sending-notifications/adding-extra-notification-channels)
- [ Customizing the notifiable ](https://spatie.be/docs/laravel-backup-rust/v1/sending-notifications/customizing-the-notifiable)

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

- [ Adding extra files to a backup ](https://spatie.be/docs/laravel-backup-rust/v1/advanced-usage/adding-extra-files-to-a-backup)
- [ Backing up a non-laravel application ](https://spatie.be/docs/laravel-backup-rust/v1/advanced-usage/backing-up-a-non-laravel-application)
- [ Binary database dumps with PostgreSQL ](https://spatie.be/docs/laravel-backup-rust/v1/advanced-usage/binary-database-dumps-with-postgresql)
- [ Encrypt backup archives ](https://spatie.be/docs/laravel-backup-rust/v1/advanced-usage/encrypt-backup-archives)
- [ Isolated mode ](https://spatie.be/docs/laravel-backup-rust/v1/advanced-usage/isolated-mode)

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

###  On this page 

1. [ Custom strategies ](#content-custom-strategies)
2. [ Notifications ](#content-notifications)

- Run `backup:clean` through your application's console, or use the bundled binary:

```
cargo run -- backup:clean
cargo run -- backup:clean --config=config/backup.json
```

- The default strategy always protects the newest backup, regardless of age or size.
- Retention windows are consecutive, not overlapping. After the all-backups window, it keeps the newest archive in each calendar day, ISO week, month and year in turn.
- The optional storage limit removes the oldest remaining archives until the limit is met, without deleting the newest archive. One oversized newest archive can exceed the limit.
- Sizes use binary megabytes (1 MiB = 1,048,576 bytes). Calendar boundaries use `backup.timezone`.

```
use spatie_backup::{Config, config::StrategyConfig};

let mut config = Config::default();
config.cleanup.default_strategy = StrategyConfig {
    keep_all_backups_for_days: 7,
    keep_daily_backups_for_days: 16,
    keep_weekly_backups_for_weeks: 8,
    keep_monthly_backups_for_months: 4,
    keep_yearly_backups_for_years: 2,
    delete_oldest_backups_when_using_more_megabytes_than: Some(5000),
};
```

Custom strategies
-----------------------------------------------------------------------------------------------------------

- Implement `CleanupStrategy` and assign it to `manager.cleanup_strategy` through your [application service binding](../installation-and-setup).
- `BackupCollection::new` and JSON deserialization sort inventories newest first. Use `.iter()` to borrow entries; collection contents cannot be reordered in place.
- Return only paths from the supplied inventory. The cleanup job rejects the newest archive and foreign paths before deleting anything on that destination.
- For example, this strategy keeps the three newest archives:

```
use chrono::{DateTime, Utc};
use spatie_backup::{BackupCollection, Result, cleanup::CleanupStrategy};

struct KeepThree;

impl CleanupStrategy for KeepThree {
    fn deletions(
        &self,
        backups: &BackupCollection,
        _now: DateTime<Utc>,
        _timezone: chrono_tz::Tz,
    ) -> Result<Vec<String>> {
        Ok(backups.iter().skip(3).map(|backup| backup.path.clone()).collect())
    }
}
```

Notifications
-----------------------------------------------------------------------------------------------

- Cleanup dispatches `CleanupWasSuccessful` or `CleanupHasFailed` per destination.
- Configure channels and recipients in the [notification guide](../sending-notifications/overview).
- `--disable-notifications` suppresses delivery without suppressing lifecycle events.

 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)
