XDEL

Introduction and Use Case(s)

The XDEL command in Redis is used to remove one or more entries from a stream. This is particularly useful for managing stream sizes and ensuring that outdated or processed entries do not consume memory unnecessarily. Typical use cases include maintaining a fixed-size stream by periodically deleting old entries and removing specific entries after they have been processed.

Syntax

  1. XDEL key ID [ID ...]

Parameter Explanations

  • key: The name of the stream from which entries are to be deleted.
  • ID: One or more stream entry IDs to be removed. Each ID uniquely identifies an entry within the stream.

Return Values

The XDEL command returns an integer indicating the number of entries that were successfully removed from the stream.

Example return values:

  • (integer) 1: One entry was successfully deleted.
  • (integer) 0: No entries were found with the specified IDs, so none were deleted.

Code Examples

  1. dragonfly> XADD mystream * field1 value1
  2. "1628847740145-0"
  3. dragonfly> XADD mystream * field2 value2
  4. "1628847741146-0"
  5. dragonfly> XRANGE mystream -
  6. 1) "1628847740145-0"
  7. 1) "field1"
  8. 2) "value1"
  9. 2) "1628847741146-0"
  10. 1) "field2"
  11. 2) "value2"
  12. dragonfly> XDEL mystream 1628847740145-0
  13. (integer) 1
  14. dragonfly> XRANGE mystream -
  15. 1) "1628847741146-0"
  16. 1) "field2"
  17. 2) "value2"
  18. dragonfly> XDEL mystream 1628847749999-0
  19. (integer) 0

Best Practices

  • Consider batch deleting entries if there are multiple IDs to remove, as this can be more efficient than issuing individual XDEL commands.
  • Combine XTRIM and XDEL to manage stream size effectively, ensuring you do not exceed your memory constraints.

Common Mistakes

  • Deleting non-existent IDs: Ensure the IDs you wish to delete actually exist in the stream to avoid unnecessary operations.
  • Confusing XDEL with XTRIM: XDEL removes specific entries by ID, while XTRIM trims the stream to a maximum length or minimum ID.

FAQs

Can XDEL remove multiple entries in a single command?

Yes, you can specify multiple IDs in a single XDEL command to delete several entries at once.

What happens if I try to delete an entry that does not exist?

If you try to delete an entry with an ID that does not exist, XDEL will simply return (integer) 0, indicating no entries were deleted.