How to remove Short URL data after 30 days

To remove short URL data after 30 days, you can create a PHP script that runs as a scheduled task (cron job). This script will query your database for short URLs that are older than 30 days and then delete them. Here's an example of how to do this:

  1. Create a PHP script, let's call it cleanup.php:
// Database connection (Replace with your own database connection details)
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

// Calculate the date 30 days ago
$thirtyDaysAgo = date('Y-m-d', strtotime('-30 days'));

// Query the database to select short URLs older than 30 days
$sql = "SELECT short_id FROM short_urls WHERE created_at < '$thirtyDaysAgo'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $shortIdentifier = $row["short_id"];
        // Delete the short URL from the database
        $deleteSQL = "DELETE FROM short_urls WHERE short_id = '$shortIdentifier'";
        // Optionally, you can perform additional cleanup or logging here


This script connects to the database, retrieves the short URLs created more than 30 days ago, and deletes them. You can also perform additional cleanup or logging tasks if needed.

  1. Set up a cron job to run the cleanup.php script periodically. The exact schedule will depend on your needs. For example, to run the cleanup script every day at midnight, you can add the following entry to your crontab:
0 0 * * * php /path/to/cleanup.php

Replace "your_server", "your_username", "your_password", and "your_database" with your actual database connection details. Also, ensure that the path to the cleanup.php script is correctly specified in the cron job entry.

By setting up this cron job, your script will regularly clean up the short URL data that is older than 30 days.

Related Articles

- All From ChatGPT