How to Safely Clean Up Old Redis Dump and Temp Files in /var/lib/redis/


2 views

When working with Redis, you'll encounter several types of data files in the /var/lib/redis/ directory:

dump*.rdb   # Snapshot files created by Redis' RDB persistence
temp-*.rdb  # Temporary files created during persistence operations
appendonly.aof  # AOF persistence file (not mentioned but relevant)

For your specific case with files like:

dump1252.rdb
dump1254.rdb
dump1256.rdb
temp-1982.rdb
temp-10259.rdb

Here's the safe cleanup approach:

  • Old dump.rdb files: You can safely delete all but the most recent dump file (check modification dates)
  • temp-*.rdb files: These are temporary files and can be safely deleted if Redis is not currently running

Before deleting anything, follow these steps:

# 1. Check Redis status
sudo systemctl status redis

# 2. Find the latest dump file
ls -lt /var/lib/redis/dump*.rdb | head -1

# 3. Verify Redis is using the correct file
redis-cli config get dbfilename

Here's a safe bash script to clean up old Redis files:

#!/bin/bash

REDIS_DIR="/var/lib/redis"
REDIS_SERVICE="redis-server"

# Keep last 3 dump files
KEEP=3

# Stop Redis if running
if systemctl is-active --quiet $REDIS_SERVICE; then
    echo "Stopping $REDIS_SERVICE..."
    sudo systemctl stop $REDIS_SERVICE
fi

# Clean up old dump files
cd $REDIS_DIR
ls -t dump*.rdb | tail -n +$(($KEEP+1)) | xargs rm -f

# Remove all temp files
rm -f temp-*.rdb

# Start Redis again
echo "Starting $REDIS_SERVICE..."
sudo systemctl start $REDIS_SERVICE

For proactive management, set up monitoring:

# Cron job to check disk space
0 * * * * df -h /var/lib/redis | awk '{print $5}' | grep -v Use | cut -d'%' -f1 | \
[ $(cat) -gt 80 ] && /path/to/redis_cleanup.sh

To prevent disk space issues in the future:

# In redis.conf
save 900 1       # Save if 1 key changed in 15 minutes
save 300 100     # Save if 100 keys changed in 5 minutes
save 60 10000    # Save if 10000 keys changed in 1 minute

# Limit RDB file generation
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes

Redis primarily uses two types of persistence files that you'll find in /var/lib/redis/:

dump*.rdb      # Point-in-time snapshots
temp-*.rdb     # Temporary files during RDB creation

The golden rule: Only delete files that aren't actively being used by Redis. Here's how to determine safety:

# Check current Redis configuration
redis-cli config get dir
redis-cli config get dbfilename

# Sample output:
1) "dir"
2) "/var/lib/redis"
1) "dbfilename"
2) "dump.rdb"

Follow this step-by-step approach:

# 1. Identify active Redis processes
ps aux | grep redis-server

# 2. Verify current dump file in use
sudo lsof | grep '/var/lib/redis/.*\.rdb'

# 3. Move (don't delete) old files for safety
mkdir /var/lib/redis/backup
mv /var/lib/redis/dump12*.rdb /var/lib/redis/backup/
mv /var/lib/redis/temp-*.rdb /var/lib/redis/backup/

# 4. Monitor Redis for issues
redis-cli ping  # Should return "PONG"

For regular maintenance, consider this bash script:

#!/bin/bash
REDIS_DIR="/var/lib/redis"
DAYS_TO_KEEP=7

# Delete old RDB files
find "$REDIS_DIR" -name 'dump*.rdb' -mtime +$DAYS_TO_KEEP -exec rm {} \;

# Always delete temp files older than 1 day
find "$REDIS_DIR" -name 'temp-*.rdb' -mtime +1 -exec rm {} \;
  • Never delete the most recent dump file if it's configured for persistence
  • Temp files created within the last few minutes might be in use
  • Consider using Redis' BGREWRITEAOF instead of manual deletion if using AOF persistence

Instead of deleting files, consider these Redis configurations:

# Reduce snapshot frequency in redis.conf
save 900 1    # After 900 sec if at least 1 key changed
save 300 10   # After 300 sec if at least 10 keys changed
save 60 10000 # After 60 sec if at least 10000 keys changed

# Or disable persistence completely (not recommended for production)
save ""