diff --git a/tools/libxl/libxl_domain.c b/tools/libxl/libxl_domain.c
index 5714501778..7f255f184c 100644
--- a/tools/libxl/libxl_domain.c
+++ b/tools/libxl/libxl_domain.c
@@ -1268,6 +1268,137 @@ static void dm_destroy_cb(libxl__egc *egc,
     libxl__devices_destroy(egc, &dis->drs);
 }
 
+static unsigned int libxl__get_max_retirement(void)
+{
+    const char *env_max_retirement = getenv("LIBXL_DOMID_MAX_RETIREMENT");
+
+    return env_max_retirement ? strtol(env_max_retirement, NULL, 0) :
+        LIBXL_DOMID_MAX_RETIREMENT;
+}
+
+static int libxl__open_domid_history(libxl__gc *gc)
+{
+    const char *name;
+    int fd;
+    int ret;
+
+    name = GCSPRINTF("%s/domid-history", libxl__run_dir_path());
+
+    fd = open(name, O_RDWR|O_CREAT, 0644);
+    if (fd < 0) {
+        LOGE(ERROR, "unexpected error while trying open %s, errno=%d",
+             name, errno);
+        goto fail;
+    }
+
+    for (;;) {
+        ret = flock(fd, LOCK_EX);
+        if (!ret)
+            break;
+        if (errno != EINTR) {
+            /* All other errno: EBADF, EINVAL, ENOLCK, EWOULDBLOCK */
+            LOGE(ERROR,
+                 "unexpected error while trying to lock %s, fd=%d, errno=%d",
+                 name, fd, errno);
+            goto fail;
+        }
+    }
+
+    return fd;
+
+fail:
+    if (fd >= 0)
+        close(fd);
+
+    return -1;
+}
+
+/* Write a domid retirement record */
+static void libxl__retire_domid(libxl__gc *gc, uint32_t domid)
+{
+    long max_retirement = libxl__get_max_retirement();
+    int fd;
+    FILE *f;
+    long roff, woff;
+    char line[64];
+    struct timespec ts;
+
+    fd = libxl__open_domid_history(gc);
+    if (fd < 0)
+        return;
+
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+
+    /* Purge old retirement records */
+
+    f = fdopen(fd, "r+");
+    woff = ftell(f);
+
+    while (fgets(line, sizeof(line), f)) {
+        unsigned long sec;
+        unsigned int ignored;
+
+        roff = ftell(f);
+
+        if (sscanf(line, "%lu %u", &sec, &ignored) != 2)
+            continue; /* Purge malformed lines */
+
+        if (ts.tv_sec - sec > max_retirement)
+            continue;
+
+        fseek(f, woff, SEEK_SET);
+        fputs(line, f);
+        woff = ftell(f);
+
+        fseek(f, roff, SEEK_SET);
+    }
+
+    fseek(f, woff, SEEK_SET);
+    fprintf(f, "%lu %u\n", ts.tv_sec, domid);
+    woff = ftell(f);
+    fflush(f);
+
+    ftruncate(fd, woff); /* may now be fewer records */
+
+    close(fd);
+}
+
+bool libxl__is_retired_domid(libxl__gc *gc, uint32_t domid)
+{
+    long max_retirement = libxl__get_max_retirement();
+    bool retired = false;
+    int fd;
+    FILE *f;
+    char line[64];
+    struct timespec ts;
+
+    fd = libxl__open_domid_history(gc);
+    if (fd < 0)
+        return false;
+
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+
+    f = fdopen(fd, "r");
+
+    while (fgets(line, sizeof(line), f)) {
+        unsigned long sec;
+        unsigned int check;
+
+        if (sscanf(line, "%lu %u", &sec, &check) != 2)
+            continue;
+
+        if (check == domid &&
+            ts.tv_sec - sec <= max_retirement) {
+            retired = true;
+            break;
+        }
+    }
+
+    close(fd);
+
+    return retired;
+}
+
 static void devices_destroy_cb(libxl__egc *egc,
                                libxl__devices_remove_state *drs,
                                int rc)
@@ -1331,6 +1462,7 @@ static void devices_destroy_cb(libxl__egc *egc,
         if (!ctx->xch) goto badchild;
 
         if (!dis->soft_reset) {
+            libxl__retire_domid(gc, domid);
             rc = xc_domain_destroy(ctx->xch, domid);
         } else {
             rc = xc_domain_pause(ctx->xch, domid);
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index cb23490c59..fcac8a93c5 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -4770,6 +4770,16 @@ _hidden int libxl__domain_pvcontrol(libxl__egc *egc,
                                     libxl__xswait_state *pvcontrol,
                                     domid_t domid, const char *cmd);
 
+/*
+ * Maximum number of seconds a domid remains in retirement after domain
+ * destruction. This can be overidden by the environment variable of the
+ * same name.
+ */
+#define LIBXL_DOMID_MAX_RETIREMENT 60
+
+/* Check whether a domid is in retirement */
+bool libxl__is_retired_domid(libxl__gc *gc, uint32_t domid);
+
 #endif
 
 /*
