How to Monitor Azure Table Storage Size and Implement Auto Cleanup

Dharmesh Joshi 41 Reputation points
2026-03-24T11:53:08.53+00:00

Hi,

My IoT devices’ data from IoT Hub is being saved in individual Azure Table Storage tables.

Over time, these tables are accumulating data, including unnecessary historical records.

Could someone please guide or advise how I can do the following:

  • Using C#, develop code that shows how many GB each table is using.
  • Apply some form of automated process on each table to remove unnecessary data after, for example, 30 or 100 days. Ideally, this should be automated.
Azure Table Storage
Azure Table Storage

An Azure service that stores structured NoSQL data in the cloud.


1 answer

Sort by: Oldest
  1. AI answer

    2026-03-24T11:53:26.73+00:00

    Azure Table Storage size and retention can be managed by combining Azure Monitor metrics with an automated cleanup process.

    To see how much space a storage account or the Table service is using, query the UsedCapacity metric from Azure Monitor. This can be done programmatically from C# via the Azure Monitor client libraries, or via CLI/PowerShell if preferred.

    Conceptually in C#:

    1. Enable metrics for the storage account (Azure Resource Manager account, not classic).
    2. Use the Azure Monitor .NET SDK to query UsedCapacity for the storage account or for the tableServices resource.
    3. Use the resource ID of the storage account or the Table service (from the Properties blade in the portal) when querying metrics.

    The documentation for monitoring Table Storage describes this pattern:

    • Metrics for Table Storage are exposed under:
      • Microsoft.Storage/storageAccounts
      • Microsoft.Storage/storageAccounts/tableServices
    • UsedCapacity is the metric that reports capacity usage.
    • From .NET, use Azure.Monitor.Query with DefaultAzureCredential to read metric definitions and values for UsedCapacity.

    For example (high level):

    • Install the NuGet packages:
      • Azure.Monitor.Query
      • Azure.Identity
    • Use the MetricsQueryClient to query:
      • Resource ID: storage account or table service
      • Metric name: UsedCapacity
      • Time grain: e.g., PT1H
    • Convert the returned capacity (bytes) to GB in code.

    For automation and cleanup of old data (for example, older than 30 or 100 days):

    1. Implement a retention rule in application code that writes to Table Storage:
      • Include a timestamp property on each entity (for example, the built-in Timestamp or a custom date column).
      • Periodically query entities older than the retention threshold and delete them.
    2. Automate the cleanup by running a scheduled process:
      • Use an Azure Function or other scheduled job that:
        • Enumerates the tables.
        • For each table, queries entities older than the threshold (for example, Timestamp < now - 30 days).
        • Deletes them in batches.
    3. Use Azure Monitor to observe the effect of cleanup:
      • Use Metrics Explorer in the Azure portal to visualize UsedCapacity and Transactions over time for the storage account or Table service.
      • Optionally filter Transactions by API name (for example, delete operations) using the ApiName dimension.
    4. If diagnostic logs are enabled to Log Analytics, Kusto queries can be used to analyze Table operations and verify cleanup behaviour, for example by querying StorageTableLogs to see delete operations over time.

    This approach provides:

    • Capacity visibility per storage account/Table service via UsedCapacity metrics.
    • Automated, time-based cleanup driven by a scheduled process that deletes entities older than the retention window.
    • Monitoring and validation via Azure Monitor metrics, Logs, and tools such as Metrics Explorer and Log Analytics.

    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.